2014-09-01 38 views
0

我想用java腳本格式化彈出文本。但無法這樣做。 我做了如下 -如何格式化java腳本中的彈出文本?

`var str = "Your changes won\'t be saved. Please press 'Cancel' to stay on the same page or 'OK' to continue."; 
var strbold = str.bold()` 
to show this msg i've done the following in the code 
     `  var retval=confirm(strbold); 
       if(retval==true){ 
       isDirty = false; 
       $(this).trigger("click");` 

,但在輸出代替味精中的粗看起來像<b>Your changes won't be saved. Please press 'Cancel' to stay on the same page or 'OK' to continue.<\b>

所以如何彈出文字的格式? p.s.我想以粗體顯示文字。

回答

1

str.bold()方法不受每個最常見的瀏覽器都支持,但是你可以使用類似這樣

function popup(t){ 
    function destroy(){ 
     document.getElementsByClassName("rtPopup")[0].remove() 
     document.getElementsByClassName("rtPopupBack")[0].remove() 
    } 
    var d1=document.createElement("div"); 
     d1.className="rtPopup" 
     d1.innerHTML="<div class='rtPopupX'>X</div>"+t 
    var d2=document.createElement("div"); 
     d2.className="rtPopupBack" 
    document.getElementsByTagName("body")[0].appendChild(div1) 
    document.getElementsByTagName("body")[0].appendChild(div2) 
    var c=document.getElementsByClassName('rtPopupX') 
    for(var i=0;i<c.length;i++){ 
     c[i].onclick=function(){destroy()} 
    } 
    document.onkeydown=function(e){ 
     if(e.keyCode==27){ 
      destroy() 
      document.onkeydown=function(e){} 
     } 
    } 
    return{ 
     destroy:function(){destroy()} 
    } 
} 

它使用一個擴展DOM對象,最重要的擴展是以下

Element.prototype.remove=function(e){ 
    for(var i=e.length-1;i>=0;i--){ 
     e[i].parentNode.removeChild(e[i]) 
    } 
} 

它將輸入作爲HTML字符串並將其顯示爲警報,您需要自己添加按鈕。 它確實需要一些CSS

.rtPopup{ 
    position:fixed; 
    top:12.5%; 
    left:12.5%; 
    height:75%; 
    width:75%; 
    z-index:1000; 
    border-radius:4px; 
    background-color:#F9F8F0; 
    margin:4px; 
} 
.rtPopup *{ 
    margin:5px; 
    margin-top:30px; 
} 
.rtPopupX{ 
    position:absolute; 
    right:5px; 
    top:-25px; 
    -moz-user-select:none; 
    -webkit-user-select:none; 
    -o-user-select:none; 
    -ms-user-select:none; 
    user-select:none; 
    cursor:pointer; 
} 
.rtPopupBack{ 
    position:fixed; 
    top:0; 
    left:0; 
    width:100%; 
    height:100%; 
    background-color:black; 
    opacity:0.5; 
    z-index:999; 
} 

距離我工作的API,可以隨意任何你想用它在哪裏。

+0

非常感謝SzenC的回覆。 但我也想保留事件,以便我可以重定向請求,如果用戶不想保存未保存的數據。這可能嗎? – 2014-09-01 10:30:38

+0

這不是直接可能的,但是您可以添加兩個按鈕(取消,保存)並將它們(onclick = function)鏈接到函數的第二部分。 – SZenC 2014-09-01 11:40:36

+0

@Victor,你可以接受這個答案。 THNX提前 – SZenC 2014-09-01 18:41:11