我有一个字段列表,其中的描述位于标签标签内。 我想添加文字"你的"在每个标签之前。
到目前为止我已经
了<div id="field1">
<label> First Label </label>
</div>
<div id="field2">
<label> Second Label </label>
</div>
<div id="field3">
<label> Third Label </label>
</div>
<div id="field4">
<label> Fourth Label </label>
</div>
var label = $("label");
$("label").html("Your"+ label.text);
https://jsfiddle.net/smftmdf1/1/
在不使用类或ID的情况下实现此目的的任何方法?
答案 0 :(得分:2)
答案 1 :(得分:1)
您提供对text
的引用时,您当前的代码不起作用,而不是使用"Your"+ label.text()
执行函数本身。但请注意,这只会将所有label
元素设置为相同的值。
要执行您需要的操作,您可以为text()
提供功能。此函数将接收当前text
值作为第二个参数。因此,您只需将Your
附加到该值即可。试试这个:
$('label').text(function(i, v) {
return 'Your ' + v;
});
答案 2 :(得分:0)
使用each()
代替html尝试text()
。我还在选择器中使用了[id * =“field”]标识符,以便它仅适用于id为fieldx
的div内的标签。
$('[id*="field"] label').each(function(){
$(this).text('Your '+ $(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="field1">
<label> First Label </label>
</div>
<div id="field2">
<label> Second Label </label>
</div>
<div id="field3">
<label> Third Label </label>
</div>
答案 3 :(得分:0)
使用.html( function )
function类型:Function(整数索引,htmlString oldhtml)=&gt; htmlString返回要设置的HTML内容的函数。收到了 集合中元素的索引位置和旧的HTML值 参数。 jQuery在调用函数之前清空元素;使用 oldhtml参数引用以前的内容。内 function,this指的是集合中的当前元素。
$("label").html(function(i,v) {
return 'Your ' + v;
});
答案 4 :(得分:0)
您需要分别使用Jquery函数来获得所需的结果。
var oExport = new sap.ui.core.util.Export({
exportType: new sap.ui.core.util.ExportTypeCSV({
separatorChar: ";"
}),
models: this.getView().getModel(),
rows: {
path: "/FaaliyetServisiSet"
},
columns: [{
name: "Kişi",
template: {
content: "{Klnad}"
}
}, {
name: "Faaliyet",
template: {
content: "{Falyt}"
}
}, {
name: "Süre",
template: {
content: {
parts: ["Sure"],
formatter: function(oValue) { // oValue is null that's the problem !!!!!!!
oValue = oValue + 2;
return oValue;
}
}
}
}, {
name: "Proje",
template: {
content: "{Proje}"
}
},
]
});
答案 5 :(得分:0)
对于您的实际标记,有一种简单的方法可以在不使用类,id或jquery开销的情况下执行此操作。
var labels = document.getElementsByTagName('label');
//OR: var labels = document.querySelectorAll('label');
for (var i = 0; i < labels.length; i++) {
labels[i].textContent = 'Your' + labels[i].textContent;
//OR: labels[i].innerHTML = 'Your' + labels[i].innerHTML;
}
另外,请考虑按标记名称批量选择不是最佳做法,并且根据您的文档结构,它可能会导致不必要的替换。如果您希望在标签内有其他标签(您希望保留),请使用innerHTML
属性。