我需要定位" Apple"在下面的html中使用下面的脚本,但它将定位父容器内的所有字符串。如何定位未包装的文字?一种解决方案是首先将文本包装在自己的子项中,但不确定如何以这种方式定位它。
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>
答案 0 :(得分:2)
您可以试用jQuery: Get the Text of Element without Child Element。
对于额外的间距,可以执行以下操作:
$(".infobar").clone().children().remove().end().text().trim()
观看演示here。
答案 1 :(得分:1)
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)
&#13;
<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>
&#13;
描述:获取匹配元素集中每个元素的子元素,包括文本和注释节点。
答案 2 :(得分:0)
也许是这样的?
$(document).ready(function() {
var ccs_cc_args = [];
ccs_cc_args.push(['FFR_NAME', $(".infobar .infobarName").html()]);
console.log(ccs_cc_args);
});
&#13;
.infobarName {
font-weight: bold;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="infobar">
<center><img src="" alt="image"/></center>
<span>Manufacturer</span> <span class="infobarName">Apple</span> <span>Part Number</span>
<div class="productboxArticlenumber">MC723-US-4-500-REF</div>
</div>
&#13;
答案 3 :(得分:0)
您可以将目标词放在span中并执行此操作
window.onload = function () {
'use strict';
var chachedWord = [];
// Select second span that exist in infobar ( 0 base index )
var targetWord = document.querySelectorAll('.infobar span')[1];
chachedWord.push('FFR_NAME' + ' ' + targetWord.textContent);
document.getElementsByTagName('p')[0].textContent = chachedWord;
};
<div class="infobar">
<center><img src="" alt="image"></center>
<span>Manufacturer</span> <span>Apple</span> <span> Part Number</span>
<div class="productboxArticlenumber">MC723-US-4-500-REF</div>
</div>
<p></p>
JQuery
var chachedWord = [];
chachedWord.push('FFR_NAME' + ' ' + $('.infobar span').eq(1).text());
$('p').text(chachedWord);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="infobar">
<center><img src="" alt="image"></center>
<span>Manufacturer</span> <span>Apple</span> <span> Part Number</span>
<div class="productboxArticlenumber">MC723-US-4-500-REF</div>
</div>
<p></p>