Get all the td tags of table which in div tag using JQUERY

╄→гoц情女王★ 提交于 2019-12-11 05:24:14

问题


Hi,

I have a div tag with id which contains table inside. I want to change the background color of td tag while mouse over and out events using Jquery. What is the way of selecting the tds with in the div using jquery?

Sample code

<div id="Outer">
   <table>
     <tr><td>1</td><td>Rama</td><td>8088080980</td></tr>
     <tr><td>2</td><td>Krishna</td><td>454546</td></tr>
   </table>
</div>

回答1:


get all td of div

$('#outer td')



回答2:


$('#Outer').delegate('td', hover, function(e) {
   if (e.type == "mouseenter") this.style.backgroundColor = "#eee";
   else this.style.backgroundColor = "";
});



回答3:


You can select all of the tds within that div with:

$("#Outer td")

Or, more generally, $("firstselector secondselector") will find all elements matching "secondselector" that are descendants of elements matching "firstselector".

However, you can achieve the desired result of changing the background colour without using jQuery or JavaScript at all - put the following in your stylesheet:

#Outer td:hover { background-color : lightblue; }​

(substitute your desired colour)

Simple demo: http://jsfiddle.net/vZ9BF/




回答4:


$('#Outer td').on('mouseenter', function() {
   $(this).css('background-color','#CCC');
}).on('mouseleave', function() {
   $(this).css('background-color','');
});

Working example here: http://jsfiddle.net/U7qeJ/



来源:https://stackoverflow.com/questions/9584450/get-all-the-td-tags-of-table-which-in-div-tag-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!