手机无法获取html元素,无法在DOM中获取h1元素的text / innerHTML(Can't get text/innerHTML of h1 element in DOM)...

无法在DOM中获取h1元素的text / innerHTML(Can't get text/innerHTML of h1 element in DOM)

我正在尝试获取两个h1元素的文本,但我的方法在执行时仍然返回“未定义”。 这是第一个h1元素

Krista Sued Coat

我获取此内容的代码

var productName = $('#window').contents().find('h1.productTitle').innerHTML;

第二个h1元素

Bliss Firm, Baby, Firm!

我获取此内容的代码

var brandName = $('#window').contents().find('.product-title font-alpha span').innerHTML;

这两者每次都返回“Undefined”。 我一直在努力解决这个问题,但没有任何作用。 如果有人能提供答案我会非常感激,如果你还能解释你的方法是如何运作的,因为我还是刚接触jquery并且想学习很多东西。

谢谢!

I'm trying to fetch the text of two h1 elements but my methods keep returning "Undefined" when executed. Here's the first h1 element

Krista Sued Coat

my code for fetching this content

var productName = $('#window').contents().find('h1.productTitle').innerHTML;

The second h1 element

Bliss Firm, Baby, Firm!

my code for fetching this content

var brandName = $('#window').contents().find('.product-title font-alpha span').innerHTML;

both of these return "Undefined" each time. I've been trying for hours to solve this issue and nothing works. If anyone can provide an answer I'd be really grateful if you could also explain how your method works since I'm still new to jquery and trying to learn a lot.

Thanks!

原文:https://stackoverflow.com/questions/15247561

更新时间:2019-12-10 21:08

最满意答案

您使用的是jQuery对象而不是DOM对象。 innerHTML是DOM对象的属性,您正在尝试将其与jQuery对象一起使用。

更改

var productName = $('#window').contents().find('h1.productTitle').innerHTML;

var productName = $('#window').contents().find('h1.productTitle')[0].innerHTML; //With javascript

要么

var productName = $('#window').contents().find('h1.productTitle').html(); //With jQuery

You are using jQuery object not DOM object. innerHTML is property of DOM object and you are trying to use it with jQuery object.

Change

var productName = $('#window').contents().find('h1.productTitle').innerHTML;

To

var productName = $('#window').contents().find('h1.productTitle')[0].innerHTML; //With javascript

or

var productName = $('#window').contents().find('h1.productTitle').html(); //With jQuery

相关问答

innerHTML评估为一个字符串。 我不知道你为什么会期望有什么不同。 考虑这个: var a = 'foo'; // now a = 'foo'

var b = a; // now a = 'foo', b = 'foo'

b = 'bar'; // now a = 'foo', b = 'bar'

重新分配b不会改变a 。 编辑补充说明:如果您不清楚上述情况,如果您想更改innerHTML ,则可以直接指定它: document.getElementById("my_div").inner

...

为什么你漂浮你的段落,但不是你的标题? 当应用于浮点数时,页边距的行为会有所不同,特别是在IE7及更低版本中。 除非你有任何真正的理由漂浮段落,否则! Why are you floating your paragraphs but not your headers? Margins behave differently when applied to floats, particularly in IE7 and below. Unless you have any real reason to

...

您使用的是jQuery对象而不是DOM对象。 innerHTML是DOM对象的属性,您正在尝试将其与jQuery对象一起使用。 更改 var productName = $('#window').contents().find('h1.productTitle').innerHTML;

至 var productName = $('#window').contents().find('h1.productTitle')[0].innerHTML; //With javascript

要么 va

...

这种情况正在发生,因为你的标题有 display:table

和h1有 display:table-cell

你的#name上有position:absolute ,它也会删除元素的宽度。 我做了一些修正,如果在这里工作。 https://jsfiddle.net/t3s2ht8n/ That's happening because your header has display:table

and h1 has display:table-cell

You have position

...

检查H1是否已经存在,并且只有在没有时才创建。 console.clear();

var el = {};

$('.placeholder').on('click', function(ev) {

$('.placeholder').css('opacity', '0');

$('.list__ul').toggle();

});

$('.list__ul a').on('click', function(ev) {

ev.preventDefault();

...

你的代码似乎没有太大的错误。 事实上,如果您声明全球h1属性,它们将用于您网站上的所有h1。 如果您创建了特定规则,那么这些规则将适用于符合该规则的任何h1,但其他属性将被继承(如果它们不同)。 我在这里更新了小提琴: http : //jsfiddle.net/lharby/ukhua6jm/2/ 例: h1 {

/* global porperties */

text-align: center;

font-family: Helvetica Neue, Helvetica,

...

getElementsByTagName返回一个类似数组的对象。 如果只有一个元素,它将返回一个带有一个元素的类数组对象。 您需要在该对象中获取[0]索引元素。 但正如您所提到的,如果您只想将样式附加到一个元素,那么在这种情况下使用id会更好。 var centeredH1 = document.getElementsByTagName("h1")[0];

// -------------------------------------------------^^^

centeredH1.st

...

您可以使用nth-of-type和subsequent sibling combinator器选择它,以便在h1之后选择第4个div ,如下所示: h1~div:nth-of-type(4) {

background-color:red;

}

〜in css被称为随后的兄弟组合程序 ,它允许您选择第一个和第二个元素类型共享父项的第二个元素类型,并且第一个元素在文档中的第二个元素类型之前。 jsFiddle: https ://jsfiddle.net/AndrewL64/crq43gs5

...

z-index仅适用于定位元素position:absolute , position:relative或position:fixed 另一个选择是让非链接h1忽略指针事件pointer-events: none; 的jsfiddle z-index only works on positioned elements position:absolute , position:relative , or position:fixed Another option would be to have t

...

添加float:left到pseudo元素就可以了。 检查更新的演示 Adding float:left to pseudo element will do it. Check updated demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值