[英]Selecting class element with Jquery
我的頁面上有一些元素。 這些是其中一些:
<div class="tile 11"></div>
<div class="tile 35"></div>
<div class="tile 89"></div>
並且我希望在我將其懸停時將 CLASS 元素放入變量中。
我試過
$(".tile").mouseenter(function(){ //on mouse over
$(this).css("background-color", "red"); //check if selected
myVariable = $(this).css; //then place css into variable
}).mouseleave(function() { //..and when I leave it
$( this ).removeAttr( "style" ); //remove color
});
但它沒有用。
例如,當我懸停第一個圖塊時,“myVariable”應該是“tile 11”,但它不是。 為什么?
這很容易。 您需要使用$(this).attr("class")
如下所示。
$(".tile").mouseenter(function(){ //on mouse over $(this).css("background-color", "red"); //check if selected var myVariable = $(this).attr("class"); //then place css into variable alert(myVariable); }).mouseleave(function() { //..and when I leave it $( this ).removeAttr( "style" ); //remove color });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="tile 11">div 1</div> <div class="tile 35">div 2</div> <div class="tile 89">div 3</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.