[英]no href, onclick function for html button
I am not too advanced in html and javascript. 我在html和javascript方面不太先进。 I am just trying to understand, how skyscanner search flights button works.
我只是想了解一下Skyscanner搜索航班按钮的工作方式。 There is no
onclick event
or function
associated with it. 没有与之关联的
onclick event
或function
。 Can anyone help me please? 谁能帮我吗?
<button class="fss-bpk-button fss-bpk-button--large js-search-button" tabindex="1" type="button">
<span class="bpk-text">Search flights</span>
<span class="bpk-icon-lg bpk-icon-pointer bpk-icon-lg--align-to-button"></span>
</button>
There is no onclick event or function associated with it.
没有与之关联的onclick事件或功能。
There probably is, it's just: 可能是,只是:
Added with JavaScript code, not with markup; 添加了JavaScript代码,而不是标记; and/or
和/或
On an ancestor element 在祖先元素上
Most likely #1. 最有可能#1。
Re #1: Using onclick
attributes and such is just one way to put an event handler on a button, and it's a way that's best avoided. 关于#1:使用
onclick
属性等只是将事件处理程序放在按钮上的一种方法,并且是最好避免的一种方法。 Instead, you use modern event handling via addEventListener
(or attachEvent
on old IE). 相反,您可以通过
addEventListener
(或旧IE上的attachEvent
)使用现代事件处理。 This might be using the DOM directly, or via a library like jQuery, MooTools, React, Vue, ... 这可能是直接使用DOM,或通过jQuery,MooTools,React,Vue等库使用的。
Example using the DOM directly: 直接使用DOM的示例:
document.querySelector(".my-button").addEventListener("click", function() { console.log("Click!"); });
<button type="button" class="my-button">Click Me</button>
Re #2: click
events bubble from the element on which the user clicked to that element's ancestor elements, all the way up to the document element ( html
). 关于#2:
click
事件从用户单击的元素一直冒泡到该元素的祖先元素,一直到文档元素( html
)。 So you can handle that event on an ancestor element, rather than on the button itself. 因此,您可以在祖先元素而不是按钮本身上处理该事件。 Handling events on an ancestor element is sometimes called "event delegation."
在祖先元素上处理事件有时称为“事件委托”。
Example: 例:
document.querySelector(".container").addEventListener("click", function(e) { console.log("Click on '" + e.target.textContent + "'!"); });
<div class="container"> This div contains two buttons. The event handler is on the div, not the buttons. <div> <button type="button">One</button> <button type="button">Two</button> </div> </div>
The script is probably called on a button holding "js-search-button" class. 该脚本可能在包含“ js-search-button”类的按钮上调用。 There is no need to insert onevent handlers within the HTML element if you can use any script to make use of it.
如果可以使用任何脚本来使用它,则无需在HTML元素内插入事件处理程序。
You can attach events using jQuery
or JavaScript
. 您可以使用
jQuery
或JavaScript
附加事件。
Below way to attach click event to static element
. 以下
attach click event to static element
。
$('button[class="fss-bpk-button fss-bpk-button--large js-search-button"]').click(function(){
console.log("clicked!!!");
});
If your element is dynamic
then you need to delegate event
. 如果您的元素是
dynamic
则需要delegate event
。
$(document).on('click', 'button[class="fss-bpk-button fss-bpk-button--large js-search-button"]', function(){
console.log("clicked!!!");
});
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.