jQuery 对象`toString()` 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15662542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:32:25  来源:igfitidea点击:

jQuery object `toString()` method

javascriptjquery

提问by Billy Moon

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

我扩展了 jQuery 对象以返回它的内部 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?

首先,除了依赖于将 jQuery 对象强制转换为字符串并匹配该字符串中的文本的类型检查之外,我看不出它会如何中断。我错了吗?

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

这将返回outerHTML集合中所有元素的 ,并连接。这对其他人有意义吗?对我来说,这很有意义。

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);

采纳答案by Pioul

Object.toStringreturns a string representing the object(from the doc).

Object.toString返回一个表示对象的字符串(来自文档)。

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

在谈到一个jQuery对象,预期的返回值Object.toString"[object Object]"

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

让它返回 HTML 只是糟糕的设计,并且可能会破坏内容。

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.

另外,根据我们想要从 jQuery 对象中检索的内容,使用不同的显式方法是有意义的:.html()对于 HTML,.text()用于剥离标签。

回答by Selvakumar Arumugam

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.

嗯,jQuery 对象不仅仅是.html. 如果 jQuery 必须实现toString,它应该足够通用以根据 jQuery 对象中的选择器返回。

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

例如,如果选择器选择了多个元素您拥有的版本将简单地返回第一个元素的 html 内容。

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

所以我想说的是,toString它并不像你想象的那么简单,我也想不出有什么好的用法toString