jQuery使用自定义字符(连字符)内爆/连接字符串(元素)
我选择了(2)个元素,通过以下方式选择:jQuery使用自定义字符(连字符)内爆/连接字符串(元素),jquery,Jquery,我选择了(2)个元素,通过以下方式选择: $(this).parents("tr:first").children("td").children("span"); 当我这样做时: $(this).parents("tr:first").children("td").children("span").text(); 跨距中的文本(比如a和b)连接在一起(到ab),这几乎就是我想要的 但是现在我想通过在两者之间插入连字符(-)来连接它们(到a-b) 我试过这个,但不起作用: $(this).pa
$(this).parents("tr:first").children("td").children("span");
当我这样做时:
$(this).parents("tr:first").children("td").children("span").text();
跨距中的文本(比如a和b)连接在一起(到ab),这几乎就是我想要的
但是现在我想通过在两者之间插入连字符(-)来连接它们(到a-b)
我试过这个,但不起作用:
$(this).parents("tr:first").children("td").children("span").join(" - ");
也许是这样的
var m = [];
$(this).parents("tr:first").children("td").children("span").each(function(index, element) {m.push(element.text());});
return m.join(" - ");
使用$.map:
$.map(
$(this).parents("tr:first").children("td").children("span"),
function(element) {
return $(element).text()
})
.join(" - ");
你可以试试,这可能会感觉有点沉重,只是没有那么复杂
$(this).parents("tr:first td span:eq(0)").text() + " - " + $(this).parents("tr:first td span:eq(1)").text()
join与数组元素一起使用。不能直接与变量一起使用。或此构造:
$(this).parents(“tr:first”).children(“td”).children(“span”).map(函数(i,元素){return$(element).text()}.get().join(“-”
$(this)
.parents('tr:first')
.children('td')
.children('span')
.append('-') // append hyphen to each span
.text();