onclick在onclick使用的另一个元素中的元素上
onclick on element that is inside other that is used by onclick too
这里有这样的问题,但我尝试了解决方案,在我的情况下,这不起作用。我有一个通过点击打开的表单,然后必须通过点击该表单内的x关闭它。这个变体不适合,因为cookie在关闭表单后没有设置为on。如果我打开,请将其设置为关闭单击。这也不起作用,因为单击x调用所有形式的单击事件。如果我真的喜欢解决方案,我会发现在stackoverflow中,表单根本不会打开。这是我的js代码:
$(document).ready(function () {
if ($.cookie('techSupport') == null) {
$.cookie('techSupport', 'off');
} else if ($.cookie('techSupport') == 'on') {
$.cookie('techSupport', 'off');
};
}).on("click", "div#techSupport", function (event) {
if ($.cookie("techSupport") == 'off') {
$.cookie("techSupport", "on");
var res = "<nobr><div>Technical support<a href='#' id='closeChat'>x</a></div></nobr>'n" +
"<div id='chatBox'>What's your probloem?</div>'n" +
"<textarea id='messageBox'></textarea>'n" +
"<button id='sendToSupport'>Send</button>";
$(this).css("width", "200px");
$(this).css("height", "400px");
$(this).html(res);
}
}).on("click", "div#techSupport,a#closeChat", function () {
var res = "<div id='techSupport'>techsupport</div>";
$("div#techSupport").html(res);
$("div#techSupport").css("width", "100px");
$("div#techSupport").css("height", "10px");
console.log($("div#techSupport").html());
$.cookie("techSupport","off");
event.stopPropagation();
//window.location.reload();
});
HTML:
<div id="techSupport">techsupport</div>
那么,我如何可以忽略主窗体点击交叉?
这里有两个选项,停止事件在a#closeChat
的onclick
中的传播(注意,我还在处理程序中添加了e
参数):
.on("click", "a#closeChat", function (e) {
var res = "<div id='techSupport'>techsupport</div>";
$("div#techSupport").html(res);
$("div#techSupport").css("width", "100px");
$("div#techSupport").css("height", "10px");
console.log($("div#techSupport").html());
$.cookie("techSupport","off");
e.stopPropagation();
//window.location.reload();
});
http://jsfiddle.net/d6fpfzsu/10/
或
检查div#techSupport
处理程序上event
的target
属性:
if ($.cookie("techSupport") == 'off' && event.target.id != 'closeChat') {
$.cookie("techSupport", "on");
var res = "<nobr><div>Technical support<a href='#' id='closeChat'>x</a></div></nobr>'n" +
"<div id='chatBox'>What's your probloem?</div>'n" +
"<textarea id='messageBox'></textarea>'n" +
"<button id='sendToSupport'>Send</button>";
$(this).css("width", "200px");
$(this).css("height", "400px");
$(this).html(res);
}
http://jsfiddle.net/d6fpfzsu/11/
最佳解决方案将取决于每个用例。
相关文章:
- 如何使用jquery迭代具有相同属性的html元素并查找onclick事件
- 如何通过它瞄准javascript元素's的onclick属性
- 将多个元素绑定到同一onclick
- 如何在动态创建元素的内联onclick事件中传递对象
- Onclick函数更改为所有更改元素的相同函数
- 为onClick-hander插入临时DOM元素
- 元素onClick无法使用ngClick
- 将列表元素 onclick 事件绑定到单选输入
- 滚动到一个元素 onClick JavaScript
- 创建 D3.js 元素 OnClick 事件的放大副本
- 单击span时获取附近的输入元素(onclick事件)
- 使用纯javascript获取li元素onclick,而不对每个元素应用onclick
- 需要通过元素onclick函数停止执行Javascript onclick函数
- 我怎么得到元素.onclick在Firefox中工作
- JavaScript服务器元素onclick事件
- 从JavaScript字符串中设置元素onclick
- Javascript的元素.onclick = functionName(作为字符串)
- 显示HTML元素onclick事件
- 选项元素onclick事件修复chrome
- 如何在不触发父元素onclick的情况下触发元素的onclick