开发者

Parent selector problem

开发者 https://www.devze.com 2023-03-14 03:27 出处:网络
Hi all I have the jq code like this: Adding element: $(\".wynink_main\").live(\"mouseenter\",开发者_运维问答 function() {

Hi all I have the jq code like this:

Adding element:

  $(".wynink_main").live("mouseenter",开发者_运维问答 function() {
  $(this).append("<div class='mapa_dojazdu'>Mapa dojazdu</div>");
  });

In div .wynik_main are some divs with text. And now when I click on div .mapa_dojazdu, I would like to get text from span .miejsce, which is append to div .wynik_main but it doesn't work.

Here is my wrong code:

$(".mapa_dojazdu").live("click", function () {
  var is =  $(this).closest("div").find(".miejsce").html();
  alert(is);
});


Change

var is = $(this).closest("div").find(".miejsce").html();

to:

var is = $(this).parent().closest("div").find(".miejsce").html();

and it should work fine. See example working here. The problem is that closest() starts with the current element, and as the current element is a div, it stops right there.


Change

$(this).closest("div").find(".miejsce").html()

to

$(this).closest(".wynink_main").find(".miejsce").html().

div is too general and will target other divs.

http://jsfiddle.net/ULjKC/

0

精彩评论

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