OnClick 可以做两件事吗? [英] Can OnClick do 2 things?
问题描述
这是html代码...
<div id="Div1"><input id="Button1" type="button" value="►"onclick="switchVisible();"style="font-size: 40px; font-weight: bold; line-height: 2; background-color: #000; text-align: center; color: #fff; margin-bottom: 0px; z-index: 102; 背景: url('.get_post_meta(get_the_ID(), 'fifu_image_url', TRUE).') 不重复;背景尺寸:封面;位置:相对;填充底部:56.25%;填充顶部:0px;高度: 0; margin-bottom: 0px; width: 100%; font-size: 120px; line-height: 5;"/><div id="Div2" class="video-embed" style="font-size: 40px; font-weight: bold; line-height: 2; background-color: #000; text-align: center; color: #fff; margin-bottom: 0px; z-index: 102;display:none;">'.do_shortcode('[video_display]').'
这是脚本...
我想在新的弹出窗口或新窗口中添加 url opening 也是 onclick 输入
类似……
https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode( get_permalink( get_the_ID() ) );?>
有可能吗?
最好的方法是使用 addEventListener
,而完全忘记 onclick
属性.删除 代码中的
onclick
并将其添加到脚本中:
document.getElementById("Button1").addEventListener("click", (e) => {/* 为东西按钮编写代码应该在点击时执行 */});
通过这种方式,您可以将 JavaScript 与 HTML 分开,从而生成更好的代码.而不是调用匿名函数 (e) =>{ ... }
或 function() { ... }
,你可以按名称调用函数,并且你可以重复这个,如果你想要多个函数在按钮时运行被点击:
document.getElementById("Button1").addEventListener("click", functionName1);document.getElementById("Button1").addEventListener("click", functionName2);/* 等等... */
使用该代码,当单击按钮时,functionName1()
和 functionName2()
都将运行.
更多信息:https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
并确保标签是
标签内的最后一个元素,这样脚本就不会中断页面的加载.
This is the html code...
<div id="video-embeds">
<div id="Div1">
<input id="Button1" type="button" value="►" onclick="switchVisible();" style="font-size: 40px; font-weight: bold; line-height: 2; background-color: #000; text-align: center; color: #fff; margin-bottom: 0px; z-index: 102; background: url('.get_post_meta(get_the_ID(), 'fifu_image_url', TRUE).') no-repeat; background-size: cover; position: relative; padding-bottom: 56.25%; padding-top: 0px; height: 0; margin-bottom: 0px; width: 100%; font-size: 120px; line-height: 5;"/>
</div>
<div id="Div2" class="video-embed" style="font-size: 40px; font-weight: bold; line-height: 2; background-color: #000; text-align: center; color: #fff; margin-bottom: 0px; z-index: 102;display:none;">'.
do_shortcode('[video_display]').'
</div>
</div>
This is the script...
<script type="text/javascript">
function switchVisible() {
if (document.getElementById('Div1')) {
if (document.getElementById('Div1').style.display == 'none') {
document.getElementById('Div1').style.display = 'block';
document.getElementById('Div2').style.display = 'none';
}
else {
document.getElementById('Div1').style.display = 'none';
document.getElementById('Div2').style.display = 'block';
}
}
}
</script>
I want to add url openning in new pop up window or new window also onclick input as
Something like...
https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode( get_permalink( get_the_ID() ) ); ?>
Is it possible?
The best way is to use addEventListener
, and just forget about the onclick
attribute altogether. Remove the onclick
in your <button>
code and add this to the script:
document.getElementById("Button1").addEventListener("click", (e) => {
/* write code for stuff button is supposed to do when clicked */
});
This way, you are separating your JavaScript from your HTML, resulting in nicer code. Instead of calling an anonymous function (e) => { ... }
or function() { ... }
, you can call functions by name, and you can do this repeatedly, if you want multiple functions to run when the button is clicked:
document.getElementById("Button1").addEventListener("click", functionName1);
document.getElementById("Button1").addEventListener("click", functionName2);
/* etc... */
With that code, both functionName1()
and functionName2()
will run when the button is clicked.
More info: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
And make sure the <script>
tag is the last element inside the <body>
tag, so that the script does not interrupt the loading of the page.
这篇关于OnClick 可以做两件事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!