使用Jquery选择类元素
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
});
但是没有成功。
例如,当我将第一个tile悬停时,"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>
相关文章:
- Webdriver.io pageObject模式-通过传递参数来定义元素选择器
- 使用元素选择器向Object添加函数
- j查询未来元素选择器
- 是否存在jQuery元素选择库的最小化版本
- Jquery动态元素选择器
- 为Select元素选择ONCHANGE
- 如何通过元素选择器获取tinyMCE编辑器实例
- 引导模式元素选择
- 下拉元素选择和 JS 调用不起作用
- 如何在 Edge Animate 中使用类元素选择器绑定 ElementAction
- 停止 Edge 浏览器尝试跨元素选择单词
- jQuery在以下选择器中引用初始元素选择器
- 编写一个类似jQuery的JS元素选择器
- 并行元素选择器
- 带有元素选择器的jQuery get()函数
- 如何设置为元素选择的属性并从其余部分移除属性
- Jquery Html元素选择器
- Jquery元素选择
- 如何使用Javascript防止HTML元素选择选项中的冲突
- 在IE上组合了活动状态伪类和相邻元素选择器