微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-在元素中定位文本而不定位子元素字符串

我需要使用下面的脚本将以下html中的“ Apple”一词作为目标,但是它将以父容器内的所有字符串为目标.如何定位未包装的文字?一种解决方案是先将文本包装在其自己的子级中,但不确定如何以这种方式定位.

ccs_cc_args.push(['FFR_NAME', $(".infobar").text()]);

HTML:

<div class="infobar">
 <center><img src="" alt="image"></center>
 <span>Manufacturer</span>Apple<span>Part Number</span>
 <div class="productBoxArticlenumber">MC723-US-4-500-REF</div>
</div>

解决方法:

var ccs_cc_args = [];

var a = $('.infobar').contents().filter(function() {
  return this.nodeType == 3;
}).text();

console.log(a)

ccs_cc_args.push(['FFR_NAME', a.trim()]);

console.log(ccs_cc_args)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="infobar">
  <center>
    <img src="" alt="Apple">
  </center>
  <span>Manufacturer</span>thisistheAppletext<span>Part Number</span>
  <div class="productBoxArticlenumber">MC723-US-4-500-REF</div>
</div>

>使用节点类型== 3表示文本.
>使用.contents

Description: Get the children of each element in the set of matched elements, including text and comment nodes.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐