2016-06-28 62 views
-1

這是我的HTML片段無法獲得通過屬性值的元素與小數(。)

<div class="style" title="Demo.1"></div> 
<div class="style" title="Sample"></div> 

我試圖使用選擇來獲得一個HTML元素

$('[title=Demo.1]').html()

和我得到了這個錯誤信息

jquery-1.8.3.js:4680 Uncaught Error: Syntax error, unrecognized expression: [title=Demo.1]

但它是爲$('[title=Sample]').html()

我認爲這是因爲小數點「。」。 是否有任何其他方式來解決這個

+1

相關部分從文檔:http://api.jquery.com/attribute-equals-selector/ – Boldewyn

+0

另一個欺騙目標:http://stackoverflow.com/questions/2694640/find-an-基於DOM的元素屬性值 –

回答

3

你的價值需要引號:

$('[title="Demo.1"]').html() 

例子:

console.log($('[title="Demo.1"]').html())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="style" title="Demo.1">Demo1</div> 
 
<div class="style" title="Sample">Sample</div>

+0

是的,它的工作,謝謝:) – Prem

0

Attribute Equals selector使用有效identifers或引用字符串通過它的值來定位適當的元素,然而引用的字符串是一般的在定位像這樣的非標識符屬性時更可靠。

// This will target any elements that have a title attribute that is exactly "Demo.1" 
$('[title="Demo.1"]').html(); 
相關問題