简体   繁体   English

.HTML()来自多个选择器

[英].HTML() from multiple selectors

I have the following jsfiddle , code below: 我有以下jsfiddle ,代码如下:

HTML: HTML:

<div id="one" style="display:none">One</div>
<div id="two" style="display:none">Two </div>
<div id="three" style="display:none">Three</div>
<div id="output"></div>

jQuery: jQuery的:

$("#output").html($("#one,#two").html());

I can make it work with one selector, what I am trying to do is use multiple selectors, I seem to be going about it the wrong way. 我可以使它与一个选择器一起工作,我想做的是使用多个选择器,我似乎正在以错误的方式进行操作。 Can someone point me in the right direction? 有人可以指出我正确的方向吗?

Thanks 谢谢

.html() will only get the content from the first element matching the selector rule: .html()将仅从与选择器规则匹配的第一个元素中获取内容:

Get the HTML contents of the first element in the set of matched elements. 获取匹配的元素集中第一个元素的HTML内容。

Source: http://api.jquery.com/html/ 资料来源: http//api.jquery.com/html/


If you want content from two elements you should do: 如果要从两个元素中获取内容,则应该执行以下操作:

$("#output").html($("#one").html() + $("#two").html());

See Demo: http://jsfiddle.net/sshg7/2/ 参见演示: http : //jsfiddle.net/sshg7/2/

You can use .text() if you only want to get text from those divs: 如果只想从这些div中获取文本,则可以使用.text()

$("#output").html($("#one, #two, #three").text())

Demo 演示版

or: 要么:

$("#output").html(function() {
  var html = [];
  $("#one,#two").each(function() {
     html.push(this.innerHTML);
  });
  return html.join("");
});

If there are lots of elements: 如果有很多元素:

$("#output").html(
  $(selector_for_many_elements).map(function () {
    return $(this).html();
  }).get().join(', ');
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM