[英]Eliminating text lines from a result of a JS pull
So I have a script that pulls an complete address block to be placed within a field of my choosing that works but now I am trying to eliminate unneeded parts of the resulting block of text for display. 因此,我有一个脚本,可以将完整的地址块放入要选择的有效字段中,但是现在我试图消除显示的文本块中不需要的部分。
The code: 编码:
<script>
Sys.Application.add_load(
function SetShipInfoValue()
{
var obj = document.getElementById("StepArea_UpdatePanelAddressBlock");
if(obj == null)
{
var y = document.getElementsByClassName('ShippingAddressBlock');
obj = y[0];
}
if(obj != null)
{
var txt = obj.innerHTML;
var obj2 = document.getElementById("FIELD_1841");
if(obj2 != null)
{
obj2.value = txt;
}
}
});
</script>
The result: 结果:
<div class="ShippingAddressBlock">
<div>Store 169</div>
<div>General Manager</div>
<div>1740 East Rd</div>
<div></div>
<div>Santa Clara, CA 92705</div>
<div></div>
<div></div>
<div>169</div>
</div>
How would I eliminate lines of text from the result to just display just the store number within the JS? 我如何从结果中消除文本行以仅显示JS中的商店编号?
You can do this by getting the last child from .children
: 您可以通过从
.children
获取最后一个孩子来做到这一点:
var ele = document.getElementsByClassName("ShippingAddressBlock")[0]; console.log(ele.children[ele.children.length-1].innerHTML); // 169
<div class="ShippingAddressBlock"> <div>Store 169</div> <div>General Manager</div> <div>1740 East Rd</div> <div></div> <div>Santa Clara, CA 92705</div> <div></div> <div></div> <div>169</div> </div>
You can also use: 您还可以使用:
console.log(ele.lastChild.previousSibling.innerHTML);
Which will be supported in older browsers. 较旧的浏览器将支持该功能。
You can use Jquery filter like this https://jsfiddle.net/DIRTY_SMITH/em0z4p84/2/ 您可以使用像这样的Jquery过滤器https://jsfiddle.net/DIRTY_SMITH/em0z4p84/2/
<div class="ShippingAddressBlock">
<div>Store 169</div>
<div>General Manager</div>
<div>1740 East Rd</div>
<div></div>
<div>Santa Clara, CA 92705</div>
<div></div>
<div></div>
<div>169</div>
</div>
<script>
$(".ShippingAddressBlock > div").filter(function(index) {
if (index === 0) {
$(this).attr('id', 'show');
}
});
</script>
CSS 的CSS
.ShippingAddressBlock > div {
display: none;
}
.ShippingAddressBlock > #show {
display: inline;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.