2017-10-06 100 views
0

为什么这段代码不工作?Button onclick =“show_popup()”不起作用

<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>'); 

但我想只有函数调用,当我点击了按钮。

+2

'show_popup'和'show_push_popup'是不一样的名字。 – apsillers

+1

show_push_popup或show_popup? –

+0

@ Pv-Viana抱歉,这是我的问题的错误 – Jishnuraj

回答

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/

+0

这可行。但它隐藏了所有其他的内容。如何解决它? – Jishnuraj

+0

@Jishnuraj看这个https://jsfiddle.net/pvviana/bpvxLmv4/ –

+0

这是一个很好的做法,已经定义了特定部分的html。以'div'标签为例 –

1

设置你的

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

<head></head> 
+0

对不起,我的问题出现了错误。我试着将web_push.js的函数名改为show_popup()。但它仍然无法正常工作。 – Jishnuraj

+0

我更新了答案 – GGO

1

变化

<button onclick="show_popup()"> 

<button onclick="show_push_popup()"> 
+0

对不起,这是我的问题的错误,请检查编辑的问题。但它仍然不起作用 – Jishnuraj

+0

将脚本放入 – plmn

1

它应该是

<button onclick="show_push_popup()"> 
+0

对不起,这是我的问题的错误,请检查编辑的问题。但它仍然不起作用 – Jishnuraj