简体   繁体   English

如何格式化Java脚本中的弹出文本?

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

I want to format popup text using java script. 我想使用Java脚本格式化弹出文本。 but unable to do so. 但无法做到。 I did as follow - 我做了如下-

 `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" );`

but in the output instead of msg in the bold it appears something like <b>Your changes won't be saved. Please press 'Cancel' to stay on the same page or 'OK' to continue.<\\b> 但在输出中而不是粗体显示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>

so how to format the popup text? 那么如何设置弹出文本的格式? ps i want text in bold. 附言:我想要加粗的文字。

the str.bold() method is not supported by every of the most common browsers, however you could use something like this 并非每种常用浏览器都支持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()}
    }
}

It uses an extended DOM object, the important extension is the following 它使用扩展的DOM对象,重要的扩展如下

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

It takes input as an HTML string and displays it as an alert, you'll need to add the buttons yourself. 它将输入作为HTML字符串并将其显示为警报,您需要自己添加按钮。 It does require some CSS 它确实需要一些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;
}

It is from an API I'm working on, so feel free to use it where ever you want. 它来自我正在使用的API,因此随时随地可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM