javascript
  • html
  • pre
  • 2016-08-04 77 views 2 likes 
    2

    您好我需要改變這個雙引號括起來,但我沒有收到怎麼辦預編碼字符串更改爲雙引號

    現在它就像

    "<iframe src='"+urlSrv+'&embedded=true'+ "' frameborder='0' scrolling='auto' width='100%' height='100%'></iframe>" 
    

    和輸出

    <iframe src='http://localhost:46030/Login/Appointment%20Booking/Home.aspx?clhid=717c043d-126f-4f57-910b-247a83d58801?embedded=true' frameborder='0' scrolling='auto' width='100%' height='1000'></iframe> 
    

    ,但我需要它作爲

    <iframe src="http://localhost:46030/Login/Appointment%20Booking/Home.aspx?clhid=717c043d-126f-4f57-910b-247a83d58801&embedded=true" frameborder="0" scrolling="auto" width="100%" height="1000"></iframe> 
    

    下面的腳本,我將其轉換

    dvContSrvBtnCopy.className = "col-lg-2 col-md-2 col-sm-2 col-xs-2 padddiv"; 
            dvContSrvBtnCopy.style.marginTop = "-2rem"; 
            var pre = document.createElement('pre'); 
            pre.className = 'precode'; 
            var code = document.createElement('code'); 
            if (type == "ServiceCatg") { 
             code.id = "txtFrameSrv_" + i; 
             code.innerHTML = '&lt;iframe src="'+urlSrv+'&embedded=true'+ '" frameborder="0" scrolling="auto" width="100%" height="1000" &gt;&lt;/iframe&gt;'; 
            } 
    
            pre.appendChild(code); 
            dvContSrv.appendChild(pre); 
            var btnCopy = document.createElement("button"); 
            btnCopy.className = 'btnCOPYOnlineBkDynamic'; 
            btnCopy.type = "button"; 
            if (type == "ServiceCatg") { 
             btnCopy.id = "btnCopySrv_" + i; 
             btnCopy.setAttribute('onclick', "StaffSrvCrclCopy('" + btnCopy.id + "','ServiceCatg',true);"); 
    
            } 
    
            var spnCopy = document.createElement("span"); 
            spnCopy.className = 'copyDOOnlineBk'; 
            btnCopy.appendChild(spnCopy); 
            dvContSrvBtnCopy.appendChild(btnCopy); 
    
    +0

    我並不完全明白的問題,其中的字符串是從哪裏來的? –

    +0

    爲什麼在JS字符串中使用'<'?你可以有實際的符號,而不是HTML實體... – evolutionxbox

    +0

    它不是給實際的符號時,給自身 – Tanmay

    回答

    2

    這是否解決問題了嗎?

    '<iframe src="' + urlSrv + '" frameborder="0" scrolling="auto" width="100%" height="1000"></iframe>' 
    

    我發現最簡單的方法是用單引號將HTML嵌入到HTML中,然後在HTML中繼續使用雙引號。

    如果您需要在HTML中使用單引號,你能逃脫他們是這樣的:

    \' 
    
    +1

    要添加到這一點,字符串可以使用單或雙引號被引用在JavaScript中沒有區別:''我的字符串'和''我的字符串''是一樣的。但是使用一個字符可以使用字符串中的另一個字符。 – pie3636

    +0

    是的,你是對的,我只是現在只有我自己得到答案,但非常感謝謝謝非常感謝謝謝 – Tanmay

    1

    如果你想雙碼,你可以使用轉義符。

    "&lt;iframe src=\"" + urlSrv + "\"&embedded=\"true\" frameborder=\"0\" scrolling=\"auto\" width=\"100%\" height=\"100%\"&gt;&lt;/iframe&gt;" 
    
    1

    如果您已經有單引號字符串,並希望用雙引號來代替單,你可以使用string.replace()

    var s2 = s.replace(/'/g, "\""); 
    

    對於一個完整的例子:你有這兩個pre標籤都有效:

    <pre id="content1"></pre> 
    <pre id="content2"></pre> 
    

    然後下面的JavaScript將填補第一個用單引號字符串,而第二個用雙引號Ø NE:

    var urlSrv = "http://localhost/"; 
    var s = "&lt;iframe src='"+urlSrv+'&embedded=true'+ "' frameborder='0' scrolling='auto' width='100%' height='100%'&gt;&lt;/iframe&gt;"; 
    
    var el = document.getElementById("content1"); 
    el.innerHTML = s; 
    
    // Here we replace all the single quotes with double qutes 
    var s2 = s.replace(/'/g, "\""); 
    
    var el2 = document.getElementById("content2"); 
    el2.innerHTML = s2; 
    

    See here for a running example

    相關問題