2012-03-12 55 views
9

是否可以在JavaScript確認框中插入HTML代碼?html中的確認框?

我只是想&它似乎並沒有工作(我嘗試添加HTML鏈接),請參閱下面

<html> 
    <head> 
    <script type="text/javascript"> function show_alert() { confirm(<a href="www.url.com">Link text</a>); } </script> 
    </head> 
    <body> 
    <input type="button" onclick="show_alert()" value="Show alert box" /> 
    </body> 
</html> 

我的代碼,我懷疑我可能會使用一個庫如"jquery-ui's dialog"得到這個工作

+1

無論如何,我只會使用jQuery-UI對話框或其他選項。遠離內置的確認/警告框。您將以這種方式獲得更多功能。 – 2012-03-12 16:49:40

回答

11

只能將明文插入alert(),confirm()和prompt()框。

1

js確認框中不可能包含HTML

你是對的,以爲你將不得不使用對話框jquery插件。

要獲得類似於確認/警告框的效果,您將不得不創建模態對話框。

2

不,這不適用於警報功能。但是,如果您要確認帶有html代碼的框,您可以在屏幕上添加一個帶有絕對位置的<div>,其中可以包含任何您想要的html。

1

HTML將不會被解析並顯示在confirm框中。

0

如果你可以使用jquery,請嘗試下面的代碼,這可以讓你有可能根據你的要求做代碼工作。在JavaScript

函數定義:

<script> 
     function ConfirmMessage(title, message) { 
      $('#confirmboxpopup').fadeIn(500, function() { 
       $('#confirmbox').animate({ 'top': '100px' }, 50); 
      }); 
      $('#confirmboxtitle').html(title); 
      $('#confirmboxmessage').html(message); 
      $('#confirmbox').show(50); 
      $('#confirmboxok').attr('onclick', okFunction()); 
      $('#confirmboxcancel').attr('onclick', onCancelClickFunction()); 
     } 

     function okFunction() 
     { 
      'enter code here to implement next step if user agree' 
     } 

     function onCancelClickFunction() 
     { 
      'enter code here to fadeout for id #confirmboxpopup to remove popup.' 
     } 
</script> 

定義標籤HTML id爲:

<div id="confirmboxpopup" class="confirmboxpopup" style="display: none"></div> 
<div id="confirmbox" class="confirm_message"> 
     <div id="confirmboxtitle" class="title"></div> 
     <div id="confirmboxmessage" class="message"></div> 
     <div id="confirmbuttons" style="float: right; padding-top: 7px"> 
      <button id="confirmboxok" type="button">ok</button> 
      <button id="confirmboxcancel" type="button">cancel</button> 
     </div> 
    </div> 

調用函數: 你只需要調用ConfirmMessage與一些參數,如「ConfirmMessage(標題,消息) 」。

+0

這不是同步程序流程,你的'ConfirmMessage'不能根據用戶選擇返回'true'或'false'。 – 2016-01-08 12:57:41