javascript中返回false和event.preventDefault有什么不同?
我搜索了一个类似的问题,有人声称javascript中返回false和event.preventDefault有什么不同?,javascript,events,javascript-events,Javascript,Events,Javascript Events,我搜索了一个类似的问题,有人声称return false类似于event.stopPropagation()和event.preventDefault()。但我已经尝试了声称的例子,它不起作用。代码如下 <ul> <li> <a href="">baidu.com</a> </li> </ul> 这是js的代码 如果返回false,则同时为event.stopPropagation()和ev
return false
类似于event.stopPropagation()
和event.preventDefault()
。但我已经尝试了声称的例子,它不起作用。代码如下
<ul>
<li>
<a href="">baidu.com</a>
</li>
</ul>
这是js的代码
如果返回false,则同时为event.stopPropagation()
和event.preventDefault()
它只会发出警报('a'),但最后会发出三次警报。根据您的代码。
如果对“a”使用event.preventDefault()。如果您有任何链接,它将不会重定向到该链接。默认操作被阻止
如果对“a”使用event.stopPropagation()。只有“a”点击事件将被触发,其余将被停止
我需要解释“返回错误”吗?
防止默认行为e.preventDefault()
防止触发其他事件处理程序(在父元素和子元素上)e.stopPropagation()
防止对同一元素触发其他事件处理程序e.preventImmediatePropagation()
用于执行上述所有操作返回false
请注意,
return false
已被弃用,因此请尝试使用事件的方法。您的代码中没有包含足够的内容,我无法理解您想做什么-但以下是对您询问的术语的解释:
event.preventDefault()
事件.停止播放()
event.stopImmediatePropagation()
三个jQuery函数在阻止事件和处理程序执行的方式上略有不同
- event.PreventDefault()用于防止元素的默认行为。例如,如果它是一个
,它将不再可单击,并且绑定到它或
代码的任何处理程序都不会被触发onclick=“”
- event.stopPropagation()和event.stopImmediatePropagation()只是略有不同。除了停止元素的默认行为外,当您希望防止事件冒泡到DOM树上时,还可以使用event.stopPropogation()和event.stopImmediatePropagation(),以防止任何父处理程序收到事件通知。如果使用event.stopImmediatePropagation(),它不仅会阻止偶数的执行,并阻止偶数冒泡到dom中的父元素,而且还会阻止将来调用该元素的任何处理程序。例如,如果它是
,它将不再可单击,并且您将无法在以后绑定将来的行为,例如
,而不强制刷新页面onclick=“”
返回false代码>
另一方面,可以说是存在于许多编程语言中的基本编程约定,Javascript就是这些语言之一
- 首先,与jQuery示例不同,它不是一个函数<代码>返回表示返回值(通常是变量或函数的输出)。第二部分只是一个布尔值
它可能与上述jQuery函数相关联的一个原因是,它经常用于内联html代码中,如false
然后在页面上的其他地方,您可以使用如下脚本:<a onclick="window.open(this.href); return false;" href="https://some_website.com/">Go To Website</a>
或 或 或$("a.some_class").on("click", function(e) { e.preventDefault(); // now the link won't go to http://some-other-website.com // but you can still bind other behavour to the element such as alert // console log or trigger another function });
$(“a.some_类”)。在(“单击”上,函数(e){ 如果(!foo){ return false;//如果变量未定义或为null,则可以直接退出函数而不执行任何代码 }否则{ a、 href=foo; $(“a.some_class”).trigger(“click”);//但是,如果定义了变量,我们将使用该变量的值来更改元素的href重定向用户浏览器的位置 } });
在jQuery中是这样做的,在vanilla JS中,你需要使用事件的方法。@Teemu没有jQuery标记,问题文本中没有jQuery,代码中没有,那么你如何在这里得到jQuery?@RobG这是“return false
“@teemu请看Tim S的答案。当然,请解释一下“return false”的含义。您列出的函数作为jQuery函数是DOM函数的副本。jQuery复制大多数DOM函数,以便可以在jQuery对象上调用它们,如有人说返回false既是event.stopPropagation()又是event.preventDefault()的唯一解释.
复制DOM方法。看看蒂姆的答案。另请参见DOM事件和。非常详细的答案+1$(…)。单击()
function validateForm() { var subject = document.forms["contact"]["subject"].value; var message = document.forms["contact"]["message"].value; var name = document.forms["contact"]["name"].value; var email = document.forms["contact"]["email"].value; if ( subject == null || subject == "" || subject == " " || message == null || message == "" || message == " " || name == null || name == "" || name == " " || email == null || email == "" || email == " " ) { $('#form-incomplete').html('<strong>Please fill out all the fields before sending email</strong>'); return false; } }
<a class="some_class" href="http://some-other-website.com">WEBSITE LINK</a>
$("a.some_class").on("click", function(e) { e.preventDefault(); // now the link won't go to http://some-other-website.com // but you can still bind other behavour to the element such as alert // console log or trigger another function });
$("a.some_class").on("click", function(e) { e.stopPropogation(); // now the link won't go to http://some-other-website.com // and the other elements of the DOM aren't aware that it's been clicked // but we can still bind other behaviour like we could: alert("user not directed to http://some-other-website.com"); });
$("a.some_class").on("click", function(e) { e.stopPropogation(); // now the link won't go to http://some-other-website.com // and the other elements of the DOM aren't aware that it's been clicked // but we can't bind other behaviour to the element. // If we try: alert("user not directed to http://some-other-website.com"); // it no longer alerts });
$("a.some_class").on("click", function(e) { if (!foo) { return false; // if our variable is undefined or null we can directly exit our function without executing any code } else { a.href = foo; $("a.some_class").trigger("click"); // however, if our variable is defined we use that variable's value to change where the href of the <a> element will redirect the user's browswer } });