[英]Selecting tricky HTML Element using JavaScript
I have some HTML inside of a Magento Theme that I am not allowed to edit however I need to come up with some JavaScript to target a really hard to target item and change it's content. 我在Magento主题内有一些HTML,不允许编辑,但是我需要拿出一些JavaScript来定位很难定位的项目并更改其内容。
Below is the basic structure of the area I have to work with, I remove all the Siblings and only the relevant code is shown now. 下面是我必须处理的区域的基本结构,我删除了所有同级兄弟,现在仅显示相关代码。
This line <label for="options_2074">None</label>
will always have a ID Number so I cannot use that to target my elements. 此行<label for="options_2074">None</label>
始终会有一个ID号,因此我不能用它来定位我的元素。
But I need to Change the text on that line from None
to Black
and also add 但是我需要将该行上的文本从“ None
更改为“ Black
,并添加
<span id="backingBlackTooltip"><img src="https://www.neonandmore.com/tooltips/question_mark.png"></span>
Right after the closing </label>
tag on that line as well. <span id="backingBlackTooltip"><img src="https://www.neonandmore.com/tooltips/question_mark.png"></span>
也在该行的</label>
标记之后。
I need some help building the JavaScript selector code to achieve this. 我需要一些帮助来构建JavaScript选择器代码来实现此目的。
Also to note, I cannot use jQuery on this project =( 另请注意,我无法在此项目上使用jQuery =(
This is the main stuff I can use... #product-options-wrapper .input-box:first .options-list + .label label
这是我可以使用的主要内容。 #product-options-wrapper .input-box:first .options-list + .label label
Any help appreciate. 任何帮助表示赞赏。 There is a JSFiddle set up here.... http://jsfiddle.net/jasondavis/0b61L398/ 这里设置了一个JSFiddle。... http : //jsfiddle.net/jasondavis/0b61L398/
<div class="product-options" id="product-options-wrapper">
<dl class="last">
<dt><label>Backing</label></dt>
<dd>
<div class="input-box">
<ul id="options-2074-list" class="options-list">
<li>
<input type="radio" id="options_2074" class="radio product-custom-option" name="options[2074]">
<span class="label">
<label for="options_2074">None</label>
</span>
</li>
</ul>
</div>
</dd>
</dl>
</div>
This should do it. 这应该做。 Make sure not to run this code until the document has loaded: 确保在文档加载之前不要运行此代码:
var alreadyFound=false;
[].forEach.call(document.getElementsByTagName('label'),function(curElt){
if(!alreadyFound&&curElt.getAttribute('for') && curElt.getAttribute('for').indexOf('options_') === 0 && curElt.textContent === 'None'){
alreadyFound=true;
curElt.textContent='Black';
var newSpan=document.createElement('span');
newSpan.setAttribute('id','backingBlackTooltip');
var newImg=new Image();
newImg.setAttribute('src','https://www.neonandmore.com/tooltips/question_mark.png');
newSpan.appendChild(newImg);
curElt.parentNode.appendChild(newSpan);
}
});
Working example: http://jsfiddle.net/0b61L398/12/ 工作示例: http : //jsfiddle.net/0b61L398/12/
What I'm doing: 我在做什么:
<label>
element and start iterating through them 获取所有<label>
元素并开始遍历它们 for
attribute, the for
attribute starts with options_
, and the content of the <label>
is None
对于每个选项,检查是否有以下工作:它具有for
属性, for
属性以options_
开头,并且<label>
的内容为None
<label>
to Black
如果满足所有这些条件,请将<label>
的内容设置为“ Black
<span>
, and set it's id
attribute to backingBlackTooltip
创建一个新的<span>
,并将其id
属性设置为backingBlackTooltip
<img>
, set it's src
to the url you provided, then add it as a child of the <span>
we just created 创建一个新的<img>
,将其src
设置为您提供的url,然后将其添加为我们刚刚创建的<span>
的子代 <img>
) to the parent of the <label>
, effectively the same as adding it "right after the closing </label>
tag" 将跨度(及其子级<img>
)附加到<label>
的父级,实际上与“在结束</label>
标签之后立即添加”相同 Also, it has been edited so that it stops after finding the first <label>
that matches all criterion. 另外,它已经过编辑,以便在找到与所有条件匹配的第一个<label>
之后停止。 jsfiddle link has been updated to reflect this change jsfiddle链接已更新以反映此更改
You will need to do something like the following: 您将需要执行以下操作:
var labelElement = document.getElementById("product-options-wrapper"). //#product-options-wrapper
getElementsByClassName("input-box")[0]. //.input-box:first
getElementsByClassName("options-list")[0]. //.options-list
getElementsByClassName("label")[0]. //.label
getElementsByTagName("label")[0]; //label
// Change the label content
labelElement.textContent = 'Black';
// Create the image and set its src
var img = new Image();
img.src = 'https://www.neonandmore.com/tooltips/question_mark.png';
// Create the span to wrapper the image
var span = document.createElement("span")
span.id = "backingBlackTooltip";
span.appendChild(img);
// Append it to the label parent element
labelElement.parentNode.appendChild(span);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.