简体   繁体   English

你为什么不能在$(this)上调用outerHTML?

[英]Why can't you call outerHTML on $(this)?

When you want to get the HTML of an entire DOM element (wrapper included), you can do the following (as explained here ): 当你想要得到的HTML整个DOM元素(包括包装),你可以做以下(如解释在这里 ):

$('#myElementId')[0].outerHTML

But what you can't do is call outerHTML on $(this) inside eg a click listener or selector function body scope: 但你不能做的是在$(this)内部调用outerHTML ,例如点击监听器或选择器函数体范围:

$(this).outerHTML //Doesn't complete in IntelliSense, returns undefined in browser

or 要么

$(this)[0].outerHTML //Correction, this DOES work, but it doesn't complete in IntelliSense

because IntelliSense won't show innerHTML or outerHTML in those circumstances, although with vanilla JavaScript you can do: 因为IntelliSense在这些情况下不会显示innerHTMLouterHTML ,尽管使用vanilla JavaScript可以做到:

document.getElementById($(this).attr('id')).outerHTML

So... what's up with that? 那么......那是什么呢?

outerHTML is a DOM property; outerHTML是一个DOM属性; jQuery doesn't expose all DOM properties. jQuery不公开所有DOM属性。

If you have a jQuery object, you can only directly access those properties and methods that jQuery exposes, and vice versa for DOM objects. 如果你有一个jQuery对象,你只能直接访问jQuery公开的那些属性和方法,反之亦然。

In object-oriented terms, jQuery objects don't inherit from DOM objects, they contain them. 在面向对象的术语中,jQuery对象不从DOM对象继承,它们包含它们。

Saying $x[0] gets you the DOM object for the first element represented by a jQuery object. $x[0]可以获得jQuery对象表示的第一个元素的DOM对象。

您可以直接使用this来访问当前对象的outerHTML ,而不是间接通过$(this)因为它表示DOM对象(具有outerHTML属性),而$(this)表示jQuery对象。

this.outerHTML

jQuery selector returns an array-like jQuery object which has no outerHTML property. jQuery选择器返回一个没有outerHTML属性的类似数组的jQuery对象。

However, the jQuery resulting array contains DOM elements. 但是,jQuery结果数组包含DOM元素。
It means that you can actually access it this way. 这意味着您可以通过这种方式实际访问它。

$(".someClass")[0].outerHTML // it works for me

Update: It works for me in every browser. 更新:它适用于每个浏览器。
I can access array-like jQuery object in a click event handler as well. 我也可以在click事件处理程序中访问类似数组的jQuery对象。

$(".someClass").click(function()
{
    alert($(this)[0].outerHTML); // it works me too
});

Here is my JSFiddle: http://jsfiddle.net/13btf60p/ 这是我的JSFiddle: http//jsfiddle.net/13btf60p/

Update 2: 更新2:

OK, now I get your question. 好的,现在我得到你的问题。 It should have worked. 它应该有效。 Do you really need an IntelliSense to complete such a plain and simple construction? 您真的需要IntelliSense来完成这种简单而简单的构造吗?

I will add what I found to be the correct solution to what ended up being a simple flaw in the default Visual Studio settings for future reference. 我将添加我发现的正确解决方案,最终成为默认Visual Studio设置中的一个简单缺陷,以供将来参考。

Since I didn't want to let this go, I searched further and found out that, by default, jQuery IntelliSense is somewhat deplorable out of the box in Visual Studio 2013. 由于我不想放弃这一点,我进一步搜索并发现,默认情况下,jQuery IntelliSense在Visual Studio 2013中有点令人遗憾。

Under

Tools > Options > Text Editor > Javascript > IntelliSense > References 工具>选项>文本编辑器> Javascript> IntelliSense>参考

I set 我设置

Reference Group: "Implicit (Web)" 参考小组:“隐含(Web)”

and added an existing jQuery file. 并添加了一个现有的jQuery文件。 This solved all issues of my question and IntelliSense now suggests all members and methods correctly, although this should have simply worked out of the box instead of costing everyone a bunch of time. 这解决了我的问题的所有问题,IntelliSense现在正确地建议所有成员和方法,虽然这应该只是开箱即用而不是花费大量时间。

this.outerHTML is enough. this.outerHTML就足够了。

If you use getElementById maybe you can use: 如果你使用getElementById ,你可以使用:

var table = document.getElementById('blablabla');

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

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