最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery object `toString()` method - Stack Overflow

programmeradmin1浏览0评论

I extended the jQuery object to return it's inner HTML...

$.fn.toString = function() {
  return this.html();
};

console.log("The inner HTML is: " + $("<div>Here, <i>there</i>, everywhere</div>"));

Is there any reason why this is not the default behaviour? Does this break something?


Updated to respond to answers/comments

Firstly, I don't see how it would break much, except for type checks which rely on coercing jQuery objects into string, and matching text in that string. Am I wrong about this?

This returns the outerHTML of all elements in a set, concatenated. Does this make any sense to anyone else? To me it makes quite a bit of sense.

var li, list;

$.fn.toString = function() {
  var out;
  out = [];
  $.each(this, function(k, v) {
    return out.push($(v)[0].outerHTML);
  });
  return out.join("\n");
};

list = $("<ul>\n  <li>some <a href='/'>link</a> items</li>\n  <li>some <a href='/'>link</a> items</li>\n  <li>some <a href='/'>link</a> items</li>\n  <li>some <a href='/'>link</a> items</li>\n  <li>some <a href='/'>link</a> items</li>\n</ul>");

li = $("li", list);

console.log("The html of it..: " + li);

I extended the jQuery object to return it's inner HTML...

$.fn.toString = function() {
  return this.html();
};

console.log("The inner HTML is: " + $("<div>Here, <i>there</i>, everywhere</div>"));

Is there any reason why this is not the default behaviour? Does this break something?


Updated to respond to answers/comments

Firstly, I don't see how it would break much, except for type checks which rely on coercing jQuery objects into string, and matching text in that string. Am I wrong about this?

This returns the outerHTML of all elements in a set, concatenated. Does this make any sense to anyone else? To me it makes quite a bit of sense.

var li, list;

$.fn.toString = function() {
  var out;
  out = [];
  $.each(this, function(k, v) {
    return out.push($(v)[0].outerHTML);
  });
  return out.join("\n");
};

list = $("<ul>\n  <li>some <a href='/'>link</a> items</li>\n  <li>some <a href='/'>link</a> items</li>\n  <li>some <a href='/'>link</a> items</li>\n  <li>some <a href='/'>link</a> items</li>\n  <li>some <a href='/'>link</a> items</li>\n</ul>");

li = $("li", list);

console.log("The html of it..: " + li);
Share Improve this question edited Mar 27, 2013 at 16:01 Billy Moon asked Mar 27, 2013 at 15:20 Billy MoonBilly Moon 58.5k26 gold badges147 silver badges243 bronze badges 5
  • why don't you use .text() ? – meo Commented Mar 27, 2013 at 15:24
  • it would override this developer.mozilla.org/en-US/docs/JavaScript/Reference/… – NimChimpsky Commented Mar 27, 2013 at 15:24
  • 2 I think it would result in some really bizarre bugs, because there are a lot of situations in JavaScript that involve an implicit call to .toString(). (I realize that it'd be convenient sometimes too.) – Pointy Commented Mar 27, 2013 at 15:25
  • You might get an authoritative answer on the jQuery Forums (forum.jquery.com). But speaking personally, I'd find it annoying if that were the default behavior. – dgvid Commented Mar 27, 2013 at 15:28
  • what would be the point - how would you set it? – Daniel A. White Commented Mar 27, 2013 at 15:36
Add a comment  | 

2 Answers 2

Reset to default 10

Object.toString returns a string representing the object (from the doc).

When talking about a jQuery object, the expected returned value for Object.toString is "[object Object]".

Making it return HTML would simply be bad design, and could break stuff up.

Plus, it makes sense to have different explicit methods depending on what we want to retrieve from a jQuery object: .html() for HTML, .text() for stripping tags.

Well, jQuery object is home for lot more than just .html.If jQuery had to implement the toString, it should be generic enough to return based on the selector in jQuery object.

For example, If the selector picked multiple elements The version you have would simply return the first elements html content.

So what I am trying to say is that the toString is not simple as you think and I couldn't think of any great usage for toString as well.

发布评论

评论列表(0)

  1. 暂无评论