jQuery使用自定义字符(连字符)内爆/连接字符串(元素)

Mic*_*hel 10 jquery

我有一个(2)元素的选择,由此选择:

$(this).parents("tr:first").children("td").children("span");
Run Code Online (Sandbox Code Playgroud)

当我这样做:

$(this).parents("tr:first").children("td").children("span").text();
Run Code Online (Sandbox Code Playgroud)

跨度中的文本(比如a和b)连接在一起(到ab),这几乎就是我想要的

但是现在我想通过在它们之间插入一个连字符( - )来连接它们(到a - b)我已经尝试了这个,但是这不起作用:

$(this).parents("tr:first").children("td").children("span").join(" - ");
Run Code Online (Sandbox Code Playgroud)

aav*_*zel 21

使用$ .map:

$.map(
  $(this).parents("tr:first").children("td").children("span"), 
  function(element) {
      return $(element).text()
  })
  .join(" - ");
Run Code Online (Sandbox Code Playgroud)

  • 或者这个结构:`$(this).parents("tr:first").children("td").children("span").map(function(i,element){return $(element).text( )}).get().join(" - ")` (6认同)

Cra*_*ray 5

也许是这样的......

var m = [];
$(this).parents("tr:first").children("td").children("span").each(function(index, element)  {m.push(element.text());});  
return m.join(" - ");
Run Code Online (Sandbox Code Playgroud)