2017-10-06 99 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