JQuery遍历DOM并提取节点值

发布于 2024-08-26 12:01:35 字数 1120 浏览 4 评论 0原文

我有以下 DOM:

<div class="qtip qtip-light qtip-active">
   <div class="qtip-wrapper">
      <div class="qtip-borderTop"></div>
      <div class="qtip-contentWrapper">
         <div class="qtip-title">
            <div class="qtip-button"></div>
            **Text I need to extract**
         </div>
         <div class="qtip-content">
            <div class="tooltip">
                <div class="button-container">
                    <script type="text/javascript">
            var link = null;
            var link = $('.qtip-active > .qtip-title').val();
            $('.qtip-active > #button-container').append('<a href=\"mailto:' + link + '\">' + link + '</a>');
                </script>
                </div>
            </div>
         </div>
      </div>
      <div class="qtip-borderBottom"></div>
   </div>
</div>

但是当我希望变量 link 返回节点指定的文本时,它总是未定义。我已经阅读 jquery 文档几个小时了,但我一定遗漏了一些我的逻辑(或缺乏)。

任何帮助表示赞赏...

I have the following DOM:

<div class="qtip qtip-light qtip-active">
   <div class="qtip-wrapper">
      <div class="qtip-borderTop"></div>
      <div class="qtip-contentWrapper">
         <div class="qtip-title">
            <div class="qtip-button"></div>
            **Text I need to extract**
         </div>
         <div class="qtip-content">
            <div class="tooltip">
                <div class="button-container">
                    <script type="text/javascript">
            var link = null;
            var link = $('.qtip-active > .qtip-title').val();
            $('.qtip-active > #button-container').append('<a href=\"mailto:' + link + '\">' + link + '</a>');
                </script>
                </div>
            </div>
         </div>
      </div>
      <div class="qtip-borderBottom"></div>
   </div>
</div>

But the variable link is always undefined when I would expect it to return the text the node specified. I've been reading the jquery documentation for a few hours but I must be missing something with my logic (or lack there of).

Any help appreciated...

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

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

发布评论

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

评论(2

别把无礼当个性 2024-09-02 12:01:35

尝试:

var link = $('.qtip-active .qtip-title').text();

qtip-title 实际上并不是 qtip-active 的子项。

只是为了澄清:

$("a > b")

表示作为 a 元素的子元素的所有 b 的集合,而:

$("a b")

表示作为 b 元素的所有集合a 元素的>后代

当然,qtip-title 不是输入元素,因此请使用 text() 而不是 val()

Try:

var link = $('.qtip-active .qtip-title').text();

qtip-title isn't actually a child of qtip-active.

Just to clarify:

$("a > b")

means the set of all b that are children of a elements whereas:

$("a b")

means the set of all b elements that are descendants of a elements.

And of course qtip-title isn't an input element so use text() not val().

南烟 2024-09-02 12:01:35

jQuery 的 val 函数返回表单元素(

您的 .qtip-title 是常规 HTML 元素,因此您应该调用 jQuery 的 text() 函数获取元素的内容。


另外,您使用了错误的选择器。 <代码>A> B 是 子选择器,并且将匹配所有 B< 直接位于 A 元素内的 /code> 元素。

您需要使用 后代选择器,匹配出现在 .qtip-active 内任何位置的所有 .qtip-title 元素。


正如 ryanulit 指出的那样,您还需要将第二个选择器更改为 $('.qtip-active .button-container')

jQuery's val function returns the value of a form element (<input>, <select>, or <textarea>).

Your .qtip-title is a regular HTML element, so you should call jQuery's text() function to get the contents of the element.


Also, you're using the wrong selector. A > B is the child selector, and will match all B elements that are directly inside an A element.

You need to write $('.qtip-active .qtip-title), using the descendant selector, to match all .qtip-title elements that appear anywhere inside .qtip-active.


As ryanulit points out, you also need to change the second selector to $('.qtip-active .button-container').

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文