我有一个想要展开/收缩的div,但链接除外。我有以下代码,它可以工作,但缺少异常。确保div "expandablediv“中的所有元素和区域的最有效方法是使用jquery进行扩展/压缩,但元素除外。
$("#expandablediv").click(function () {
if ($("#withinexpandlediv").is(":hidden")) {
$("#withinexpandlediv").fadeIn(50);
}
else {
$("#withinexpandlediv").fadeOut(50);
}
});HTML代码:
<div id="expandablediv" >
<div class="ddImage">
<img src="rightArrow.png" alt="Title">
</div>
<div class="ddText">
Title
</div>
<div id="withinexpandlediv" >
Text contains one or more <a href="mailto:email@links.com"> email links.</a>
</div>
</div>发布于 2013-07-23 04:49:25
如果您的意思是不希望链接触发切换,请使用event.target.nodeName
$("#expandablediv").click(function (e) {
if (e.target.nodeName == "A") { return; }
//if ($(e.target).is('a')) { return; } // also works
if ($("#withinexpandlediv").is(":hidden")) {
$("#withinexpandlediv").fadeIn(50);
}
else {
$("#withinexpandlediv").fadeOut(50);
}
});nodeName:http://jsfiddle.net/m7vpk/
is():http://jsfiddle.net/FQuzt/
发布于 2013-07-23 04:48:24
就像这样
if(!$('#expandablediv').children().has('a')){
// handle expandsion/contraction here
}有关更多详细信息,请查看此链接:jQuery .has()
发布于 2013-07-23 05:02:05
您可以尝试这样做:
$("#expandablediv").click(function () {
if ($("#withinexpandlediv").is(":hidden")) {
$("#withinexpandlediv").fadeIn(50);
}
else {
$("#withinexpandlediv").fadeOut(50);
}
}).find('a').click(function(e){
e.stopPropagation();
});https://stackoverflow.com/questions/17797038
复制相似问题