使用 jquery 访问 td 中的 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2472875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accessing div inside the td using jquery
提问by azamsharp
not sure why I find this so difficult. Here is the html code:
不知道为什么我觉得这很难。这是html代码:
<tr>
<td>FirstName 9</td>
<td>LastName 9</td>
<td><a href="#" id="linkSponsorMessage">View</a></td>
<td>
<div class="divSponsorMessage" style="display:none">
Greetings FirstName 9, LastName 9!
</div>
</td>
</tr>
I need to access the <div>
when click on the anchor link using JQuery.
我需要<div>
使用 JQuery 单击锚链接时访问。
UPDATE:
更新:
I got to work:
我开始工作:
$("#linkSponsorMessage").parent("td").next("td").children("div")
But is there a better way!!!
但是有没有更好的方法!!!
UPDATE 2:
更新 2:
Also, since I am using multiple DIVS and anchor tags I had to do $(this) to refer to the current anchor tag that was triggered.
此外,由于我使用了多个 DIVS 和锚标记,因此我必须执行 $(this) 来引用当前触发的锚标记。
$(document).ready(function()
{
$("a").mouseover(function()
{
var divs = $(this).closest("tr").find("div").fadeIn("slow");
});
$("a").mouseout(function()
{
var divs = $(this).closest("tr").find("div").fadeOut("slow");
});
});
采纳答案by Nick Craver
You can use this:
你可以使用这个:
$("#linkSponsorMessage").closest("tr").find("div")
or:
或者:
$("#linkSponsorMessage").closest("tr").find(".divSponsorMessage")
回答by Raja
Just use
只需使用
$("a").click(function(){alert("clicked");return false;};)
The above code would show an alert clicked and would not allow the default functionality of link click to happen.
上面的代码会显示一个被点击的警报,并且不允许链接点击的默认功能发生。
Hope this helps.
希望这可以帮助。
回答by Gabriele Petrioli
scrap my original answer if you have multiple div messages.... thought you used an id for both the link and the div
如果您有多个 div 消息,请废弃我的原始答案.... 以为您对链接和 div 都使用了 id
look at Nick Craver's answer on how to find the div by proximity ..
看看 Nick Craver 关于如何通过接近度找到 div 的答案..
[original answer]
【原答案】
Since you use IDs for each element it is very straight-forward to access anything.. here is some sample code..
由于您为每个元素使用 ID,因此访问任何内容都非常简单。这里是一些示例代码。
$('#linkSpnsoMessage').click( function(){
var $div = $('.divSponsorMessage');
//$div variable now holds the jquery version of the div..
} );
回答by mcgrailm
try this
尝试这个
$(td a).click(function(){
alert('hello');
return false;
});
回答by Gabe
$('div.divSponsorMessage');