jQuery 获取innerHTML 不适用于 HTMLFontElement 对象

发布于 2024-08-25 18:46:59 字数 476 浏览 0 评论 0原文

我有 jQuery 来选择所有 font 元素,这些元素是存储在 var html 中的 html 中具有 id="right" 的元素的子元素。 ..当我发出警报以查看它获得了多少个元素时:

alert($("#right > font", html).length);

它给了我一个警报:5

但是当我尝试以下任何操作时,我没有收到任何警报...

alert($("#right > font", html)[0].html());
alert($("#right > font", html)[0].text());
alert($("#right > font", html)[0].val());

任何有想法吗?

谢谢,
马特

I have jQuery to select all font elements that are children of the element with id="right" within the html stored in the var html... when I do an alert to see how many elements it gets:

alert($("#right > font", html).length);

it gives me an alert of: 5

but when I try any of the following, I don't get any alerts...

alert($("#right > font", html)[0].html());
alert($("#right > font", html)[0].text());
alert($("#right > font", html)[0].val());

Any Ideas?

Thanks,
Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

前事休说 2024-09-01 18:46:59

由于该元素不是 jQuery 的实例,它只是一个 DOM 对象,因此您不能使用任何 jQuery 方法。您可以使用innerHTML 来获取结果。

alert($("#right > font", html)[0].innerHTML);

如果你想对元素应用任何 jQuery 方法,你必须将其设置为 jquery 对象,例如

alert($($("#right > font", html)[0]).html());

$("#right > font").each(function(){
    alert($(this).html());
});

Since the element is not an instance of jQuery, and its just a DOM object you can't use any of the jQuery methods. You can use innerHTML to get the result.

alert($("#right > font", html)[0].innerHTML);

If you want to apply any of the jQuery methods to the element you have to make it a jquery object like

alert($($("#right > font", html)[0]).html());

or

$("#right > font").each(function(){
    alert($(this).html());
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文