JQuery:如何从一个对象获取属性 [英] JQuery: How to get attribute from an object
问题描述
我从一个对象中获取innerHTML时遇到了问题。目前我有这样的代码:
$ $ p $ console.log($(myString).find(#+ someID)。 prevObject);
- myString是HTML代码的字符串变量
- someID是要获取的HTML标记的当前ID。在此示例中,它会从匹配ID的字符串中获取
< li>
元素。
这将在控制台中输出以下内容:
我正在努力研究如何从此日志中获取 innerHTML
属性。
干杯
您可以获取元素的HTML通过jQuery html
功能。因此,假设具有ID的元素确实存在:
console.log($(myString).find(#+ someID ).html());
这会给你想说的你想要的: li的HTML
与 id
。请注意,我删除了 prevObject
。原因有二:
-
你说你想要
li
与id 。如果你这样做,你不希望 prevObject
。 -
prevObject
是jQuery的未记录的方面。它可以改变含义,更改名称,或完全从一个点释放到下一个点。 (但主要是第一点)。
I am having a problem getting the innerHTML from an object. At the moment I have this code:
console.log( $(myString).find("#" + someID).prevObject );
- myString is a string variable of HTML code
- someID is the current ID of the HTML tag to get. In this example, it gets a
<li>
element from the string with the matching ID.
This outputs the following in the console:
I am just struggling to work out how to get the innerHTML
attribute from this log.
Cheers
You can get the HTML of an element via the jQuery html
function. So assuming the element with the ID really exists:
console.log( $(myString).find("#" + someID).html() );
That will give you what you said you wanted: The HTML of the li
with that id
. Note that I removed the prevObject
. Two reasons for that:
You said you wanted the HTML of the
li
with thatid
. If you do, you don't wantprevObject
.prevObject
is an undocumented aspect of jQuery. It can change meaning, change name, or go away entirely from one dot release to the next. (But mostly point #1.)
这篇关于JQuery:如何从一个对象获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!