gpt4 book ai didi

c# - 无法在 javascript 中从 C# HttpUtility.JavaScriptStringEncode 解码字符串

转载 作者:行者123 更新时间:2023-11-30 18:30:14 24 4
gpt4 key购买 nike

我正在通过 C# 生成 html

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 脚本是:

$('[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 class=\u0027abcd\u0027 title=\u0027September 05, 2013 12:06\u0027\u003e\u003ci

如何在工具提示中呈现 html?这一直在消耗我的时间和大脑...请帮忙!

注意:在将此问题标记为重复之前,请阅读以下内容:
我搜索了所有相关的帖子,但没有一个解决方案对我有用。我的用例不同,因为我使用 qtip 显示由 javascriptstringencode 生成的字符串。

最佳答案

我找不到任何内置函数来解码使用 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>

关于c# - 无法在 javascript 中从 C# HttpUtility.JavaScriptStringEncode 解码字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21884598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com