如何将 p 的当前内容替换为新的内容?我当前的解决方案不起作用。响应会返回,因此如果我在 function(response) {} 中放入一个警报(“Something”),它就会起作用。内容没有被替换,那就是什么也没有发生!
$(this).find(".caption").find("#yes").live('click', function() {
$.ajax({
url: 'prob.php',
data: {action: 'yes'},
type: 'post',
success: function (response) {
$(this).find(".caption").find("#change").html("<p>Come on!</p>");
}
});
});
这是我的 html:
<li>
<a class="thumb" name="leaf" href="<?php echo $images_dir.$_SESSION['current_img'].".jpg" ?>" title="Title #0">
<img src="<?php echo $images_dir.'thumbs/'.$_SESSION['current_img'].".jpg" ?>" alt='Title #0' />
</a>
<div class="caption" id="current">
<div class="image-title">
<p id="change">Image shows a nautillus shell:
<input type="checkbox" id="yes">Yes
<input type="checkbox" id="no">No
</p>
</div>
</li>
最佳答案
你真的需要遍历 DOM 树吗?因为您需要的两个元素都有一个(希望)唯一的 ID,这意味着您可以直接访问它们。
Here是一个简化的 js 代码。
$(document).ready(function () {
$('#yes').on('click', function () {
$('#change').text('Come on!');
});
});
如果这对你来说不可能,我还有另一个建议,因为其他人已经提到了你的问题 $(this)
在给定的上下文中。
我建议您将 .live() 替换为 .on()因为.live()从 jQuery v1.7 开始已弃用,并在 1.9 中删除
编辑
我还设置了一个fiddle其中#yes 和#change 已更改为类。在这个 fiddle 中你有 2 <li>
标签和“来吧!”文本被插入到右侧 <p class"change">
.
关于jquery - 使用 jQuery 更改 p 标签的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16267577/