我想使用标签名称将ID属性添加到标签中。使用jQuery。
<label class="coral-Form-fieldlable">Width</label>
<label class="coral-Form-fieldlabel">Height</label>
<label class="coral-Form-fieldlabel">Quality</label>
<label class="coral-Form-fieldlabel">Color List</label>
在Dom中,存在具有相同类名class=“珊瑚形式字段”的多个类。我想通过使用label name = "Width“只为一个而不是所有的'label‘添加ID属性。
发布于 2021-03-26 14:14:37
这很简单,你可以使用下面的代码。
$(document).ready(function(){
$(".coral-Form-fieldable").each(function(i,o){
var current=$(this);
current.attr("id",current.text());
console.log(current.attr("id"));
})
})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="coral-Form-fieldable">Width</label>
发布于 2021-03-26 13:42:47
你可以试试这个
另外,请100%使用此链接查看您的解决方案dynamically change label 'for' attribute in with Jquery
$("input, select").each(function(){
var fieldName = $(this).attr('name');
$(this).siblings().attr('for', fieldName);
});发布于 2021-03-26 13:55:10
你可以试试这个
function getElementByValue(selector, value) {
let elements = document.querySelectorAll(selector);
for (let element of elements) {
if (element.innerText.indexOf(value) != -1) {
return element;
}
}
return null;
}
// Call it
getElementByValue(".coral-Form-fieldable","Width")https://stackoverflow.com/questions/66811536
复制相似问题