为什么 jQuery 添加反斜杠“\”我的字符串中的字符?
我使用 jQuery 将元素的内容替换为字符串,其中包含一些元素。以下是我用来创建 $value 字符串的代码:
var value = '<span class="fn">' + $("#fn_").val() + '</span>';
$("input", this).val(value);
但是,实际输出的内容全部都有退格字符,如下所示:
<span class="\"fn\"">Name</span>
我假设这可能是我的一个愚蠢错误,但不确定我做错了什么?感谢任何建议!
编辑
为了清楚起见,这里是完整的代码,我正在为 jEditable 创建一个自定义输入类型,它允许我内联编辑 vcard。
$.editable.addInputType('person', {
element : function(settings, original) {
var fn = $('<input id="fn_"/>');
var bday = $('<input id="bday_" />');
var wday = $('<input id="wday_" />');
var dday = $('<input id="dday_" />');
var spouse = $('<input id="spouse_" />');
$(this).append(fn);
$(this).append(bday);
$(this).append(wday);
$(this).append(dday);
$(this).append(spouse);
/* Hidden input to store value which is submitted to server. */
var hidden = $('<input type="hidden">');
$(this).append(hidden);
return(hidden);
},
content : function(string, settings, original) {
var $data = $(string);
var data = {};
$data.each(function () {
var $t = $(this);
data[$t.attr('class')] = {
class: $t.attr('class'),
value: $t.text()};
});
alert(data.length);
$("#fn_", this).val('Name');
$("#bday_", this).val('Born');
$("#wday_", this).val('Married');
$("#dday_", this).val('Died');
$("#spouse_", this).val('Spouse');
},
submit: function (settings, original) {
var value = "<span class=fn>" + $("#fn_").val() + '</span>' + '<span class=spouse>' + $("#spouse_").val() + '</span>' + '<span class=bday>' + $("#bday_").val() + '</span>' + '<span class=wday>' + $("#wday_").val() + '</span>' +'<span class=dday>' + $("#dday_").val() + '</span>';
$("input", this).val(value);
}
});
I'm using jQuery to replace the contents of an element with a string, which contains some elements. The following is the code I'm using to create $value, the string:
var value = '<span class="fn">' + $("#fn_").val() + '</span>';
$("input", this).val(value);
However, what actually gets outputted has backspace characters all through it, like this:
<span class="\"fn\"">Name</span>
I'm assuming this is probably a stupid mistake on my part, but not sure what I'm doing wrong? Appreciate any advice!
EDIT
For clarity, here's the full code, I'm creating a custom input type for jEditable that allows me to inline edit a vcard.
$.editable.addInputType('person', {
element : function(settings, original) {
var fn = $('<input id="fn_"/>');
var bday = $('<input id="bday_" />');
var wday = $('<input id="wday_" />');
var dday = $('<input id="dday_" />');
var spouse = $('<input id="spouse_" />');
$(this).append(fn);
$(this).append(bday);
$(this).append(wday);
$(this).append(dday);
$(this).append(spouse);
/* Hidden input to store value which is submitted to server. */
var hidden = $('<input type="hidden">');
$(this).append(hidden);
return(hidden);
},
content : function(string, settings, original) {
var $data = $(string);
var data = {};
$data.each(function () {
var $t = $(this);
data[$t.attr('class')] = {
class: $t.attr('class'),
value: $t.text()};
});
alert(data.length);
$("#fn_", this).val('Name');
$("#bday_", this).val('Born');
$("#wday_", this).val('Married');
$("#dday_", this).val('Died');
$("#spouse_", this).val('Spouse');
},
submit: function (settings, original) {
var value = "<span class=fn>" + $("#fn_").val() + '</span>' + '<span class=spouse>' + $("#spouse_").val() + '</span>' + '<span class=bday>' + $("#bday_").val() + '</span>' + '<span class=wday>' + $("#wday_").val() + '</span>' +'<span class=dday>' + $("#dday_").val() + '</span>';
$("input", this).val(value);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,我似乎通过使用
而不是
删除类名周围的引号来完成这项工作。由于某种原因,引号被正确地放回输出中,但是我无法使用多个类。
Hmm I seem to have got this working by using
instead of
i.e. removing the quotes from around the class name. For some reason the quotes are being put back in correctly in the output, however I'm not able to use multiple classes.
如果您尝试设置跨度内的 HTML,一种正确的方法是:
或者如果您想完全替换该元素:
或者如果您只想删除类
vcard
,请添加类fn
,并设置内容:这可确保文本框中的值在插入
span
元素之前正确进行 HTML 转义。使用.val
设置元素的 HTML 内容是不正确的;为此,请使用.html
,尽管在本例中没有必要。If you are trying to set the HTML that is inside a span, one correct way to do it is:
Or if you want to replace the element entirely:
Or if you just want to remove class
vcard
, add classfn
, and set the contents:This ensures that the value from the textbox is properly HTML-escaped before it is inserted into the
span
element. It's not correct to use.val
to set the HTML contents of an element; use.html
for that, although it is not necessary in this case.您是否知道您的提交函数是在将数据发送到服务器之前执行的(查看 Jeditable 源代码),这意味着您所有的“跨度”都被发送到服务器,因此可能对您的元素进行编码的是您的服务器端脚本。
干杯。
Did you know that your submit function is executed before sending data to the server (take a look at Jeditable source), this means all your "spans" are sent to the server, so what is probably encoding your elements is your server-side script.
Cheers.