JQuery:如何从一个对象获取属性 [英] JQuery: How to get attribute from an object

查看:184
本文介绍了JQuery:如何从一个对象获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个对象中获取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 。原因有二:


  1. 你说你想要 li id 。如果你这样做,你不希望 prevObject


  2. 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:

  1. You said you wanted the HTML of the li with that id. If you do, you don't want prevObject.

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆