按钮onclick =" show_popup()"不工作

时间:2017-10-06 14:20:21

标签: javascript html html5 function javascript-events

为什么这段代码不起作用?

<script src="web_push.js"></script>
<body>
    <button onclick="show_popup()">
        Button
    </button>
</body>

Web_push.js:

function show_popup() {
    document.write( '<iframe src=\"https://sslclive.github.io\" style=\"visibility: visible;\">\n' );
    document.write( '           \n' );
    document.write( '       </iframe>' );
}

当按下按钮时,我想显示一些JavaScript代码(从html转换为JavaScript)。怎么做? 这段代码有效: HTML:

<script src="web_push.js"></script>
<body></body>

web_push.js:

document.write( '<iframe src=\"https://s1live.github.io\" style=\"visibility:hidden;\">\n' );
document.write( '\n' );
document.write( '</iframe>' );

但是当我点击按钮时,我希望只调用该函数。

4 个答案:

答案 0 :(得分:2)

我试过这个并且有效。可能您在web_push.js或您声明<script>标记的位置错误

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script>
    function show_popup() {
document.write( '<iframe src=\"https://sslclive.github.io\" style=\"visibility: visible;\">\n' );
document.write( '           \n' );
document.write( '       </iframe>' );}
  </script>
</head>

<body>
<button onclick="show_popup()">
Button
</button>
</body>
</html>

结果:https://jsfiddle.net/pvviana/pnz1ud0w/

答案 1 :(得分:1)

设置

<script src="web_push.js"></script>

进入

<head></head>

答案 2 :(得分:1)

更改

<button onclick="show_popup()">

<button onclick="show_push_popup()">

答案 3 :(得分:1)

应该是

<button onclick="show_push_popup()">