Replace html of a div after anchor tag?
This is a quick example not my actual code. Onclick of the div button
, how do i replace the html after the anchor tag or parent div title
? The jQuery code here replaces everything in the div title
i 开发者_运维百科only want to replace what is after the anchor tag.
script
$(function() {
$('.button').live('click',function() {
var info = $(this).next('.information').html();
$(this).next('.title').html(info);
});
});
html
<div class='container'>
<div class='button'>Click Me</div>
<div class='information' style='display:none;'>information</div>
<div class='title'><a href='http://www.example.com/' class='link'></a>title</div>
</div>
You should wrap that in a span
<div class='title'><a href='http://www.example.com/' class='link'></a><span>title</span></div>
and target that with
$(function() {
$('.button').live('click',function() {
var self = $(this);
var info = self.nextAll('.information').html();
self.nextAll('.title').find('span').html(info);
});
});
There are a couple of things I changed and I think this jsFiddle is close to what you need - http://jsfiddle.net/FloydPink/LHPT2/
1) The $title
should probably be within the anchor tag
2) The information
div should be before the button since you're using .prev()
Try this:
<script type="text/javascript">
$(function() {
$('.button').live('click', function() {
var info = $(this).prev('.information').html();
$(this).next('.title').children('a').text(info);
});
});
</script>
<?php
while ($row=mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$information = $row['information'];
echo "<div class='container'>
<div class='information' style='display:none;'>$information</div>
<div class='button'>Click Me</div>
<div class='title'><a href='http://www.example.com/' class='link'>$title</a></div>
</div>";
}
?>
instead of working with prev()
and next()
, try to work with siblings()
$('.button').live('click',function() {
var info = $(this).siblings('.information').html();
$(this).siblings('.title').html(info);
});
精彩评论