c#
  • qtip2
  • 2014-02-19 15 views 3 likes 
    3

    我生成通過C#無法從C#HttpUtility.JavaScriptStringEncode在JavaScript

    myStr = "<span class='red'>September 1980</span><br /><div>abcdef\nhijklm</div>"; 
    shtml = "<span class='red' title='<pre>" + HttpUtility.JavaScriptStringEncode(myStr, false) + "</pre>' id='" + jc.FirstOrDefault().UserId + "'>" + content + "</span>" + after; 
    ... snip snip ...  
    <%= shtml %> 
    

    我的初始化qtip jQuery腳本的HTML字符串解碼爲:

    $('[title!=""]').each(function(){ 
            $(this).qtip({ 
             hide: { 
              fixed: true, delay: 300 
             }, show: 'mouseover', 
             position: { 
              my: 'top center', 
              at: 'bottom center', 
              viewport: $(window), 
              adjust: { 
               method: 'shift shift' 
               , screen: true 
              } 
             }, style: { 
              classes: 'qtip-light', // Inherit from preset style 
              tip: 'topCenter' 
             } 
            }); 
    
           }); 
    

    現在,工具提示顯示:
    \ u003cspan類= \ u0027abcd \ u0027標題= \ u0027September 05,2013 12:06 \ u0027 \ u003e \ u003ci

    我怎樣才能使HTM在工具提示? 這一直在吃我的時間和大腦...請幫助!

    注意:請在將此問題標記爲重複之前閱讀以下內容:
    我搜索了所有相關帖子,但沒有一個適用於我。我的用例不同,因爲我使用qtip來顯示由javascriptstringencode生成的字符串。

    回答

    0

    我找不到任何內置函數來解碼使用HttpUtility.JavaScriptStringEncode編碼的數據。所以我在各個網站進行了一些研究之後創建了一個JS函數。

    String.prototype.replaceAll = function(str1, str2, ignore) { 
     
        return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof(str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2); 
     
    } 
     
    
     
    function decodeJs(encodedString) { 
     
        var decodedString = encodedString; 
     
        decodedString = decodedString.replaceAll("\\u0026", "&"); 
     
        decodedString = decodedString.replaceAll("\\u0027", "\'"); 
     
        decodedString = decodedString.replaceAll("\\u003c", "<"); 
     
        decodedString = decodedString.replaceAll("\\u003e", ">"); 
     
        return decodedString; 
     
    } 
     
    
     
    function replaceText() { 
     
        $("*").each(function() { 
     
        if (!$(this).children().length) { 
     
         $(this).text(decodeJs($(this).text())).val(decodeJs($(this).val())); 
     
        } 
     
        }); 
     
    } 
     
    $(document).ready(replaceText); 
     
    $("html").ajaxStop(replaceText);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    相關問題