2016-08-18 115 views
0

我在我的c#代碼中創建了一個元素,它包含一個將字符串參數複製到剪貼板的onclick事件。如何將包含HTML的字符串傳遞給onClick事件?

它工作正常,問題是當我必須傳遞一個HTML代碼參數,
和標籤。它打破了我的頁面,因爲可能出現的引號(')(如style和colspan標籤)。

我正在使用的代碼是這樣的:

string comment = row.Cells[1].Text; 

Cells[0].Text += "<br/><a href='javascript:' onclick='javascript:CopyComment(" + comment + ")'>COPY</a>"; 

我如何傳遞一個字符串,HTML作爲參數,而不是打破我的網頁?

編輯1:

這是HTML代碼我傳遞的參數:

> "<span style='color:#FF0000;'><b>COTAÇÃO</b></span><br />\r\n<br 
> />\r\n<br />\r\nAssunto: Cotação/Assistência: 425595/Placa: 
> EAL8426<br />\r\n<br />\r\nSegue conforme solicitado:<br />\r\n<br 
> />\r\n<span 
> style='text-decoration:underline;'><b>ORIGEM:</b></span><br />\r\nRua 
> Glauber Rocha, 39 - Jardim Alzira, São Paulo - SP, 03986-270, Brasil, 
> 39<br />\r\n<br />\r\n<span 
> style='text-decoration:underline;'><b>DESTINO:</b></span><br />\r\nR. 
> Conselheiro Saraiva, 36 - Santana, São Paulo - SP, Brasil, 36<br 
> />\r\n<br />\r\n<br />\r\n<b>Cotação 1</b><br />\r\nTempo de Chegada: 
> 50<br />\r\n<br 
> />\r\n<table>\r\n<tr>\r\n<td>Tarifa</td><td>Valor</td><td>Quantidade</td><td>Total</td>\r\n</tr>\r\n<tr>\r\n<td>Saída</td><td>R$ 
> 99,00</td><td>1</td><td>R$ 99,00</td>\r\n</tr>\r\n<tr>\r\n<td>KM 
> (Excedente)</td><td>R$ 1,50</td><td>10</td><td>R$ 
> 15,00</td>\r\n</tr>\r\n<tr>\r\n<td>Pedágio</td><td>R$ 
> 20,00</td><td>1</td><td>R$ 20,00</td>\r\n</tr>\r\n<tr>\r\n<td>Horas 
> Trabalhadas</td><td>R$ 46,00</td><td>2</td><td>R$ 
> 92,00</td>\r\n</tr>\r\n<tr>\r\n<td>KM (Deslocamento)</td><td>R$ 
> 1,50</td><td>10</td><td>R$ 
> 15,00</td>\r\n</tr>\r\n<tr>\r\n<td>Destombamento</td><td>R$ 
> 125,00</td><td>1</td><td>R$ 125,00</td>\r\n</tr>\r\n<tr>\r\n<td 
> colspan='2'>&nbsp;</td><td>Total</td><td>366,00</td>\r\n</tr>\r\n</table>\r\n<br 
> />\r\n\r\n<br />\r\nNo aguardo<br />\r\n<br />\r\nAtenciosamente\r\n" 

這是我的JavaScript函數複製文本(用HTML格式)到剪貼板

function CopiarComentario(html) { 
    var tmpEl; 
    tmpEl = document.createElement("div"); 

    // since we remove the element immediately we'd actually not have to style it - but IE 11 prompts us to confirm the clipboard interaction and until you click the confirm button, the element would show. so: still extra stuff for IE, as usual. 
    tmpEl.style.opacity = 0; 
    tmpEl.style.position = "absolute"; 
    tmpEl.style.pointerEvents = "none"; 
    tmpEl.style.zIndex = -1; 

    // fill it with your HTML 
    tmpEl.innerHTML = decodeHtml(html); 

    // append the temporary node to the DOM 
    document.body.appendChild(tmpEl); 

    // select the newly added node 
    var range = document.createRange(); 
    range.selectNode(tmpEl); 
    window.getSelection().addRange(range); 

    // copy 
    document.execCommand("copy"); 

    // and remove the element immediately 
    document.body.removeChild(tmpEl); 
} 

function decodeHtml(html) { 
    var txt = document.createElement("textarea"); 
    txt.innerHTML = html; 
    return txt.value; 
} 
+0

你有沒有試過把它作爲[verbatim sting](http://stackoverflow.com/a/3312007/4416750)傳遞? –

回答

1
Cells[0].Text += "<br/><a href='javascript:' onclick='javascript:CopyComment(" + HttpUtility.HtmlEncode(comment) + ")'>COPY</a>"; 

Read more about HttpUtility.HtmlEncode here.

+0

我的JavaScript函數複製到剪貼板需要一個HTML代碼,所以它保持格式以防人員想要將文本放入電子郵件中,例如。 –

+0

您需要在您的CopyComment函數中解碼HTML。這裏是如何做到這一點的示例http://stackoverflow.com/questions/7394748/whats-the-right-way-to-decode-a-string-that-has-special-html-entities-in-it – serhiyb

+0

從未想過要在客戶端解碼。 但是現在當我點擊時出現這個錯誤:Uncaught SyntaxError:Unexpected token} 但是我的代碼沒有'}'。 –

相關問題