关于 jQuery 中的parent()和prev()的非常基本的问题

发布于 2024-09-18 02:09:28 字数 757 浏览 3 评论 0原文

我正在使用 JavaScript 函数处理超链接的点击事件。我想从超链接中检索数据。

页面如下所示:

<div class="div1">
   <div title="Title 1" class="div2">
      <p class="p1"><a class="linkJs">The link in question</a></p>
   </div>
</div>

<div class="div1">
   <div title="Title 2" class="div2">
      <p class="p1"><a class="linkJs">The link in question</a></p>
   </div>
</div>

JavaScript 如下所示:

$(function() {
   $('a.linkJs').click(function(){
      value=$(this).parent().prev().text();
      alert(value);
   });
});

我想要的是 div2 中 TITLE 的值。通过单击第一个链接,我得到:标题 1。单击第二个链接:标题 2。

这一定是非常非常基本的,但我在任何地方都找不到答案。

谢谢。

I am handling a hyperlink's click event with a JavaScript function. I want to retrieve data from the hyperlink.

The page looks like this:

<div class="div1">
   <div title="Title 1" class="div2">
      <p class="p1"><a class="linkJs">The link in question</a></p>
   </div>
</div>

<div class="div1">
   <div title="Title 2" class="div2">
      <p class="p1"><a class="linkJs">The link in question</a></p>
   </div>
</div>

And the JavaScript something like this:

$(function() {
   $('a.linkJs').click(function(){
      value=$(this).parent().prev().text();
      alert(value);
   });
});

What I want to have is the value of the TITLE in the div2. By clicking the first link I get : Title 1. And by clicking on the 2nd: Title 2.

This must be very very basic but I just can't find my answer anywhere.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

守不住的情 2024-09-25 02:09:29

您想使用 closest

var value = $(this).closest('div').attr('title');

您的问题是 < ;p> 标签不是

的同级标签,而是子标签,因此您必须执行 parent() 两次 - 没有必要不过,因为 closest 函数是一个方便的快捷方式。另外,text()函数返回标签内部的纯文本内容,如果您想要标签的title属性,则需要使用attr函数。

You want to use closest

var value = $(this).closest('div').attr('title');

Your problem is that the <p> tag is not a sibling to the <div> but a child, so you would have to do parent() twice - there's no need, though, as the closest function is a handy shortcut. Also, the text() function returns the pure text contents inside the tag, if you want the title attribute of the tag you need to use the attr function.

手心的海 2024-09-25 02:09:29

您可以使用 closest 方法找到它:

   $('a.linkJs').click(function(){
      value=$(this).closest('div').attr('title');
      alert(value);
   });

You can find it with the closest method:

   $('a.linkJs').click(function(){
      value=$(this).closest('div').attr('title');
      alert(value);
   });
奶茶白久 2024-09-25 02:09:29

尝试使用 closest() 方法: http://api.jquery.com/最接近/

获取第一个祖先元素
匹配选择器,从
当前元素和进展
通过 DOM 树。

$(function() {
    $('a.linkJs').click(function(){
        value=$(this).closest(".div2").attr("title");
        alert(value);
    });
});

try using the closest() method: http://api.jquery.com/closest/

Get the first ancestor element that
matches the selector, beginning at the
current element and progressing up
through the DOM tree.

$(function() {
    $('a.linkJs').click(function(){
        value=$(this).closest(".div2").attr("title");
        alert(value);
    });
});
盛装女皇 2024-09-25 02:09:29
var value = $(this).closest('.div2').attr('title');

.div2 可能是更合适的选择器,而不是使用 div,因为 div.div2 内可能还有其他 div 元素。

var value = $(this).closest('.div2').attr('title');

Instead of using div, .div2 may be a more appropriate selector because there may be other div elements inside div.div2.

窗影残 2024-09-25 02:09:29

不是很漂亮,但这应该也可以。

$(function() {
    $('a.linkJs').click(function(){
        value=$(this).parents()[1];
        alert($(value).attr('title'));
    });
});

Not very pretty, but this should work too.

$(function() {
    $('a.linkJs').click(function(){
        value=$(this).parents()[1];
        alert($(value).attr('title'));
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文