Tim*_*Tim 3 javascript jquery onclick
有没有办法将onclick-function的返回值confirm转换为jQuery函数?
我有链接,其中有一个onclick事件进行确认,返回true或false:
<a onclick="return confirm('Sure?');" class="delete" href="delete.php">Delete</a>
Run Code Online (Sandbox Code Playgroud)
这是一个给定的结构,我没有可能改变它.但我想做这样的事情:
$('a.delete').click(function() {
if (confirm_from_onclick == true) {
//do some more before really returning true and following the link
}
});
Run Code Online (Sandbox Code Playgroud)
如果您无法更改HTML标记,则需要onclick从该节点中删除完整的内联处理程序,并在不显眼的事件处理程序中删除它.
这可能看起来像:
$(function() {
$('a').each(function(_, anchor) {
var onclk = anchor.onclick; // store the original function
anchor.onclick = null; // delete/overwrite the handler
$(anchor).bind('click', function() {
if( onclk() ) {
// do something
}
return false; // call stopPropagation() + preventDefault()
});
});
});
Run Code Online (Sandbox Code Playgroud)
由于您正在处理onclick内联事件处理程序,因此无法阻止或阻止使用不引人注意的绑定事件处理程序进行触发.它将始终首先触发,因此您需要完全删除原始事件.
你并不真的需要存储的功能,你也可以只设置onclick到null并改写自己的事件处理程序中的逻辑.我这样做只是为了方便.
| 归档时间: |
|
| 查看次数: |
2978 次 |
| 最近记录: |