2015-12-12 88 views
0

我試圖實現的是從同一元素中的另一個屬性獲取屬性。例如:如何從同一元素中的另一個屬性獲取屬性

<script> 
$("#step1").click(function() { 
    // Get the attribute 'next' and get its value. 
}); 
</script> 

<button id="step1" next=".openNextModal"> 
+3

FYI:'next'不是一個按鈕 – adeneo

+0

有效的屬性我想製作自定義元素屬性的正確方法是使用數據 - 前綴。我知道'next'不是一個有效的屬性。 @adeneo – FocuZst

+0

使用'data-next'來代替這是一個有效的HTML5自定義數據屬性。 – www139

回答

3

你需要使用attr()

<script> 
$("#step1").click(function() { 
    // Get the attribute 'next' and get its value. 
    var next = $(this).attr('next'); 

    //if you are looking for next attribute value of the next element    
    console.log($(next).attr('next')); 

    //if this is an input element and you are looking for its value 
    console.log($(next).val()); 

    //if this is an not input element and you are looking for its html 
    console.log($(next).html()); 

}); 
</script> 
2

請嘗試如下。

$("#step1").click(function() { 
    console.log($(this).attr('next')); 
}); 
相關問題