javascript 带有onclick事件的javascript弹出框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32990638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:59:34  来源:igfitidea点击:

javascript popup box with onclick event

javascripthtmlcss

提问by Madhusudan Yadav

I am looking for the JavaScript code that can assist me in getting something similar to the button onclick event. Please take a look at the code below.

我正在寻找可以帮助我获得类似于按钮 onclick 事件的 JavaScript 代码。请看下面的代码。

Javascript code

Javascript代码

<script type='text/javascript'>
function myPopup() {

    var overlay = $('<div id="overlay"></div>');
    $('.x').click(function() {
        $('.popup').hide();
        overlay.appendTo(document.body).remove();
        return false;
    });
    var obj = document.getElementsByClassName('clickLink');

    // alert("name");
    myScript = function() {
        overlay.show();
        overlay.appendTo(document.body);
        $('.popup').show();
        return false;
    };

    obj[0].addEventListener("click", myScript);

}
</script>

HTML Code

HTML代码

<div class='popup'>
    popup content here
</div>

<div id='container'>
    <button class='clickLink'>
        Click Here to See Popup!
    </button>
    <!-- this is working 5ne i called class in javascript -->

    <button type="submit" onclick='myPopup();'>
        Click Here to See Popup!
    </button>
    <!-- but developers need like onclick event -->

</div>

</body>

This popup script is working fine but I need an onclickevent like button onClick='myfunction()' please any one help me Thanks

此弹出脚本运行良好,但我需要一个onclick事件,如按钮 onClick='myfunction()' 请任何人帮助我 谢谢

回答by Sizons

If I understand correctly you want to know how onclick event works in JavaScript, if that is the case as your question is not entirely clear, try something to the effect of the following:

如果我理解正确,您想知道 onclick 事件在 JavaScript 中是如何工作的,如果是这种情况,因为您的问题不完全清楚,请尝试以下效果:

<!DOCTYPE html>
<html>
<body>

<p>Click "Try it" to execute the displayDate() function.</p>

<button id="myBtn">Try it</button>

<p id="demo"></p>

<script>
document.getElementById("myBtn").onclick = displayDate;

function displayDate() {
    document.getElementById("demo").innerHTML = Date();
}
</script>

</body>
</html>