0

我想解决一个问题,我将一个 onclick 元素附加到一个 div (并且只附加到它!)

请看以下代码:

<script>
function  goToo(url,idd){
alert(idd);
if (idd=="onlyMe")
window.location=url;
}
</script>
<div id="onlyMe" onclick="javascript:goToo('http://www.google.com', this.id)" style="<-index:-150">
<form><input type="text" name="coap"></form>

<h3>This is an element where click should work!</h3>

<div id="notME">
<h3>In this point it should not work!</h3>
</div>

</div>

我希望仅在 div 单击时触发 onlick。

请现场查看示例@http: //modacalcio.com/HtmlProblemForm.html

到处都会触发点击,特别是在表单输入中。

Obiovusly 我希望在不删除子节点中的 onclick 的情况下使用它,因为它们有自己的 onclick 仍然需要工作

还有 jquery 我有同样的问题有什么帮助吗?

4

2 回答 2

2
$('#onlyMe').on('click', function(evt) {
    if ($(evt.target).parents('#notME').length === 0 && evt.target.id !== 'notME') {
        location.href = "http://...";
    }       
});

小提琴示例:http: //jsfiddle.net/zhkr2/2/

这个想法是检查事件是在#notME元素上还是在包含的元素内触发的#notME,所以我检查祖先是否是那个元素

于 2012-06-06T10:11:08.977 回答
0

只给'div'一些宽度

#onlyMe
{
width:
height:

}

并正确关闭 div 。

于 2012-06-06T10:15:11.103 回答