簡體   English   中英

如何格式化Java腳本中的彈出文本?

[英]how to format popup text in java script?

我想使用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" );`

但在輸出中而不是粗體顯示msg,它顯示類似<b>Your changes won't be saved. Please press 'Cancel' to stay on the same page or 'OK' to continue.<\\b> <b>Your changes won't be saved. Please press 'Cancel' to stay on the same page or 'OK' to continue.<\\b>

那么如何設置彈出文本的格式? 附言:我想要加粗的文字。

並非每種常用瀏覽器都支持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,因此隨時隨地可以使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM