2014-01-08 17 views
1

我有一個觀點有很多圖片jQuery的數據屬性得到這個元素

<img src="@Path" data-img-prev="@UrlPreview" data-full-path="@UrlImage" onclick=Get() /> 

,並在HTML中呈現這樣

<img img-prev="1.jpg" data-full-path="https://...1.jpg" src="https://...1.jpg"> 
<img img-prev="2.jpg" data-full-path="https://...2.jpg" src="https://...1.jpg"> 
<img img-prev="3.jpg" data-full-path="https://...3.jpg" src="https://...1.jpg"> 

我想函數從每個元素中獲取數據

function Get() { 
this.getAttribute('img-path'); 
this.data('img-path'); 

}

它不能正常工作,請幫助:

錯誤this.getAttribute不是一個函數

+0

你怎麼稱呼TTT()函數? ??它是Get()函數嗎? –

+0

你必須改善你的問題。什麼是呈現的HTML?很明顯,這不是你所發佈的... –

+0

給我一瞬間它如此之快 – user246340

回答

2

的數據屬性稱爲data-full-path,不img-path

var result = this.getAttribute('data-full-path'); 

或者因爲你使用jQuery標記它(assumings你在點擊處理程序上下文是):

var result = $(this).data('full-path'); 
+0

謝謝你$(this).data('full-path');它是works和this.getAttribute('data-full-path');錯誤getAttribute不是函數 – user246340

+0

@ user246340如果你不想使用jQuery,你需要檢查瀏覽器是否支持它:if('getAttribute'in this)'應該足夠了。 – Johan

+0

它是工作在ie 9-10 Firefox和鉻最後版本我只在這個瀏覽器工作 – user246340

1

使用.data('full-path')而不是.data('img-path')

相關問題