使用 jQuery 更改 ap 标签的内容

ili*_*ija 1 jquery

如何用新的内容替换 ap 的当前内容?我目前的解决方案不起作用。响应回来了,所以如果我在 function(response) {} 里面放一个 alert("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>");
                        }
                    });
                });
Run Code Online (Sandbox Code Playgroud)

这是我的 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>
Run Code Online (Sandbox Code Playgroud)

Sir*_*ton 5

你真的需要遍历DOM树吗?因为您需要的两个元素都有一个(希望)唯一的 ID,这意味着您可以直接访问它们。

是一个简化的 js 摆弄您的代码。

$(document).ready(function () {

    $('#yes').on('click', function () {

        $('#change').text('Come on!');

    });
});
Run Code Online (Sandbox Code Playgroud)

如果这对您来说是不可能的,我还有另一个建议,因为其他人已经$(this)在给定的上下文中提到了您的问题。我想建议你用.on()替换 .live ()因为.live()从 jQuery被弃用,并在 1.9 中被删除

编辑

我还设置了一个小提琴,其中 #yes 和 #change 已更改为类。在这个小提琴中,你有 2 个<li>标签和“来吧!” 文本被插入到右边<p class"change">