开发者

Why does this "next" not work?

开发者 https://www.devze.com 2023-04-05 14:16 出处:网络
I want to get data through Ajax and fill them into div .content, but it won\'t work. HTML: <div class=\"review\">

I want to get data through Ajax and fill them into div .content, but it won't work.

HTML:

<div class="review">
  <span class="seefull" rel="1">see full content</span>
  <div class="content"></div>
  <div class="c_createtime">(06-15 04:03:13)</div>
</div>

<div class="seg_r"><img src="http://img.dachaocai.com/sys/seg_2.gif"/></div>

<div class="review">
  <span class="seefull" rel="2">see full content</span>
  <div class="content">12</div>
  <div class="c_createtime">(07-31 12:46:18)</div>
</div>

jQuery:

$(function() {
  $(".seefull").click(function() {
    $.ajax({
      url: "/j/fullreview",
      data: {"t":"b","id":$(this).attr("r开发者_Go百科el")},
      success: function(data) {
        $(this).find("div .content").html(data);
      }
    })
  })
})


  $(function(){
        $(".seefull").click( function (){
            var $this = $(this);
            $.ajax({
                url:"/j/fullreview",
                data:{"t":"b","id":$this.attr("rel")},
                success:function(data){
                    $this.parent().find(".content").html(data);
                }
            });
        });
  });

It's about the context of "this".

When the ajax callback function executes, this does not refer to your original object anymore. So, you need to store its original reference on a variable, and use to that variable from there on to be sure you are always referring to the right element.

var $this = $(this);

With the above code, your object is now stored in the $this variable.

$this.parent().find(".content").html(data);

Find the parent of our element, then find the element with class "content", and change its content.

I didn't find a div of class content because from your code there is only one element with that class.


What exactly is not working?

On first blush, I believe...

$(this).find("div .content").html(data);

should be...

$(this).parent().find("div.content").html(data);

And if data is a JSON object, then the output you will see will be something like [Object] object instead of any useful data, so additional parsing of the JSON object will be needed.


change

$(this).find("div .content").html(data);

to

$(this).parent().find("div.content").html(data);

problem 1:
$(this) refers to the span that was clicked, wich doesn't contain a div. use parent() to go ane level up in the dom-tree and search for div.content there.

problem 2:
div .content means "any element wich class content inside of a div".
this should be div.content wich means "a div with class content")


It's all about the context...

 $(function(){
        $(".seefull").click(function(){
           vat that = $(this);
            $.ajax({
                url:"/j/fullreview",
                data:{"t":"b","id":$(this).attr("rel")},
                success:function(data){
                    that.next().html(data);
                }
            })
            })
        })
0

精彩评论

暂无评论...
验证码 换一张
取 消