2017-04-18 118 views
0

我有Chrome和Safari瀏覽器的問題:奇怪的行爲,jQuery的

代碼示例:

// Get all links with the same data-test attribute 
 
links = $('[data-test]'); 
 

 
//The issue is here: 
 
// This only works in Chrome, not in safari. 
 
var test = links[0].testProperty; 
 

 
// This works in Chrome and Safari. 
 
var test2 = $(links[0]).attr('testProperty'); 
 

 
console.log(test) 
 
console.log(test2)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="#" testProperty="valueTest1" data-test>Test1</a> 
 
<a href="#" testProperty="valueTest2" data-test>Test2</a> 
 
<a href="#" testProperty="valueTest3" data-test>Test3</a>

爲什麼Safari瀏覽器不允許links[0].testProperty

+0

第一個也不適用於鉻... – DaniP

+0

http://stackoverflow.com/questions/15010981/javascript-getting-custom-attribute-works-only-with-element-getattributeattrib – DaniP

+0

'testProperty '是屬性,而不是屬性 – LGSon

回答

0

links[0]是一個DOM元素對象,它沒有屬性testPropety

chrome和safari都不會定義該屬性。