简体   繁体   English

Javascript:生成ID时,需要在页面上删除类的单个实例

[英]Javascript: Need to remove a single instance of a class on the page when the id is generated

This title may be a little convoluted.... but here it goes. 这个标题可能有点令人费解。

I have this block of code. 我有这段代码。 In the (I think it's called the parent) span, you can see the id is defined as qa_prdcmpr_qtyspan_0. 在(我认为这是父级)跨度中,您可以看到id被定义为qa_prdcmpr_qtyspan_0。 There are 4 of those on a page (created as 0-3) to compare up to 4 products. 一个页面上有4个(创建为0-3)可比较4个产品。

<span id="qa_prdcmpr_qtyspan_0" data-pa-element="compare-qty-column" name="qty-input">
   <br>
   <input class="txtInput" type="text" name="shoppingCartQty_0" id="qa_prdcmpr_qty_0"      maxlength="4" style="border:1px solid;border-color:#000 grey grey #000;width:35px">
   <span width="30px" id="qa_qty_error_1375968996697" class="error">Enter a quantity.</span>
</span>

In the top level span of this block, in the id=qa_prdcmpr_qtyspan_0. 在此块的顶级跨度中,在id = qa_prdcmpr_qtyspan_0中。 The 0 is a generated number (there is 0-3 for product compare) 0是生成的数字(产品比较有0-3)

The long number on the span id is a time stamp to guarantee uniqueness. 跨度id上的长数字是确保唯一性的时间戳。 Instead of writing the page as a generic product container and being able to include x containers on a page, they defined the page with 4 hard coded containers for product comparisons. 他们没有将页面写为通用产品容器,而是可以在页面上包含x个容器,而是使用4个硬编码容器定义了页面,以进行产品比较。 That being said, I can work with that. 话虽如此,我可以配合。

I have tried this: 我已经试过了:

/* Remove Error Message if applicable */
var $errorElements = $('.error');
$(this).find($errorElements).remove();

I'm in the process of trying this: 我正在尝试这样做:

var errorNode = $('span[id^="qa_qty_error_"]').attr('id');
while (errorNode.firstChild) {
    errorNode.removeChild(errorNode.firstChild);
}

The problem with the first on is that it is removing all the "error" elements on the page if there are multiple errors. 首先出现的问题是,如果存在多个错误,它将删除页面上的所有“错误”元素。 I thought I could get rid of that by using 'this' but I guess not. 我以为我可以使用'this'摆脱它,但我想不是。

Do I have to do something with parent / child nodes to get the correct one? 我必须对父/子节点做些什么才能获得正确的节点?

EDITED for clarity (an attempt) 为了清晰起见进行了编辑(尝试)

I ended up finding a pc_prfix which is what is used to generate the 0-3 on the end of the id's. 我最终找到了pc_prfix,它是用于在ID的末尾生成0-3的内容。 It was a little deceiving as I am of the mind set of descriptive and correct variable names. 就像我对描述性和正确的变量名的看法一样,这有点欺骗。

$('#qa_prdcmpr_qty_' +pc_prfix).next('span.error').remove(); $('#qa_prdcmpr_qty_'+ pc_prfix).next('span.error')。remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM