我正在尝试获取两个h1元素的文本,但我的方法在执行时仍然返回"Undefined".这是第一个h1元素
<div id="product-details" class="floatLeft">
<h1 class="productTitle">Krista Sued Coat</h1>
</div>
Run Code Online (Sandbox Code Playgroud)
我获取此内容的代码
var productName = $('#window').contents().find('h1.productTitle').innerHTML;
Run Code Online (Sandbox Code Playgroud)
第二个h1元素
<div class="product-title-wrap">
<h1 class="product-title font-alpha">
<span itemprop="name">
Bliss Firm, Baby, Firm!
</span>
</h1>
</div>
Run Code Online (Sandbox Code Playgroud)
我获取此内容的代码
var brandName = $('#window').contents().find('.product-title font-alpha span').innerHTML;
Run Code Online (Sandbox Code Playgroud)
这两者每次都返回"Undefined".我一直在努力解决这个问题几个小时没有任何作用.如果有人能提供答案我会非常感激,如果你还能解释你的方法是如何工作的,因为我还是刚接触jquery并且想学习很多东西.
谢谢!
您正在使用jQuery对象而非DOM对象.innerHTML是DOM对象的属性,您正在尝试将其与jQuery对象一起使用.
更改
var productName = $('#window').contents().find('h1.productTitle').innerHTML;
Run Code Online (Sandbox Code Playgroud)
至
var productName = $('#window').contents().find('h1.productTitle')[0].innerHTML; //With javascript
Run Code Online (Sandbox Code Playgroud)
要么
var productName = $('#window').contents().find('h1.productTitle').html(); //With jQuery
Run Code Online (Sandbox Code Playgroud)
你可以使用jQuery的html方法:
var productName = $('#window').contents().find('h1.productTitle').html()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11348 次 |
| 最近记录: |