Javascript Jquery多行文本问题
我有一个div,里面有文本:Javascript Jquery多行文本问题,javascript,jquery,debugging,html,Javascript,Jquery,Debugging,Html,我有一个div,里面有文本: <div>A description of dreamstill. It is a music site that is social and other stuff where you cando this and that and pretty much anything that your heart allows. This is probably as long as the description will get.</div>
<div>A description of dreamstill. It is a music site that is social and other stuff where you cando this and that and pretty much anything that your heart allows. This is probably as long as the description will get.</div>
问题是图像的生成过程如下所示:
<img class="center" src="http://featherfiles.aviary.com/2011-09-09/aae3b22a952841cd9c0d6a26a5867325.png" data-desc="A" description="" of="" dreamstill.="" it="" is="" a="" music="" site="" that="" social="" and="" other="" stuff="" where="" you="" cando="" this="" pretty="" much="" anything="" your="" heart="" allows.="" probably="" as="" long="" the="" will="" get.="">
我在这里做错了什么?div.replaceWith(“”);
div.replaceWith('<img class="center" src="'+url+'" data-desc="' + desc + '" />');
忘记了desc
div.replaceWith('')两侧的“替换”;
忘记了
desc
两侧的“您缺少引号。所有属性值都应该用引号括起来
div.replaceWith('<img class="center" src='+url+' data-desc="' + desc + '" />');
来自w3c的
属性值应始终用引号括起来
现场演示:您缺少引号。所有属性值都应该在“引号中
div.replaceWith('<img class="center" src='+url+' data-desc="' + desc + '" />');
来自w3c的
属性值应始终用引号括起来
live demo:问题在于标记中的值周围缺少引号,但不能只添加引号,因为如果描述文本中有引号,则引号仍然会中断 相反,试试这个
var desc = div.text();
div.replaceWith(
$('<img />', {
"class": "center",
"src": url,
"data-desc": desc
})
);
var desc=div.text();
替换为(
$('问题在于标记中的值周围缺少引号,但不能只添加引号,因为如果描述文本中有引号,这仍然会中断
相反,试试这个
var desc = div.text();
div.replaceWith(
$('<img />', {
"class": "center",
"src": url,
"data-desc": desc
})
);
var desc=div.text();
替换为(
$('其他答案都是正确的,因为您缺少引号,但下一个问题是当描述包含引号并破坏HTML时。我的建议是附加图像,然后设置data-desc
div.replaceWith(
$('<img class="center" src="' + url + '"/>').attr('data-desc', desc)
);
div.replaceWith(
$('').attr('data-desc',desc)
);
其他答案都是正确的,因为您缺少引号,但下一个问题是当描述包含引号并破坏HTML时。我的建议是附加图像,然后设置data-desc
div.replaceWith(
$('<img class="center" src="' + url + '"/>').attr('data-desc', desc)
);
div.replaceWith(
$('').attr('data-desc',desc)
);
这将用字符实体替换引号
desc.replace(/\"/g, '"')
结果+编码url:
div.replaceWith('<img class="center" src="' + encodeURI(url) + '" data-desc="' + desc.replace(/\"/g, '"') + '" />');
div.replacetwith(“”);
这将用字符实体替换引号
desc.replace(/\"/g, '"')
结果+编码url:
div.replaceWith('<img class="center" src="' + encodeURI(url) + '" data-desc="' + desc.replace(/\"/g, '"') + '" />');
div.replacetwith(“”);
这是否有效?我认为jQuery函数的第二个参数是context元素。是的。它是在1.4中添加的。您可以在这里看到它:“jQuery(html,props)”我还需要动态地将图像包装在a
标记周围:'
我该怎么做?这行吗?我认为jQuery函数的第二个参数是context元素。是的。它是在1.4中添加的。您可以在这里看到它:作为“jQuery(html,props)”我还需要动态地将图像包装在a
标记周围:'
我该怎么做?如果'desc'中有引号,这将失败。一秒钟后,我将尝试在JSFIDLE上运行它。检查您是否有url
等所有变量。我已将实时演示添加到answerIt半成品中。如果您有'Hey'“there”作为描述,您在那里传递jQuery'data desc=“Hey”“”。浏览器中“data desc”的值现在为“Hey”,缺少一大块文本。为了更清楚地说明这个例子,请将“data desc”移到“src”之前并加上引号。现在元素完全缺少它的“src”属性。在属性中切换引号或转义数据。像这样使用div.replaceWith(“);
但是在这种情况下,你的句子中会有”
你需要逃避它,如果“desc”中有引号,这将失败。一秒钟后,我会尝试在JSFIDLE中使用它。检查你是否有像url
这样的所有变量。我已经在answerIt半成品中添加了实时演示。如果你有“嘿”作为描述,您正在传递jQuery的data desc=“Hey”“”。浏览器中“data desc”的值现在为“Hey”,缺少一大块文本。为了更清楚地说明这个例子,请将“data desc”移到“src”之前并加上引号。现在元素完全缺少它的“src”属性。在属性中切换引号或转义数据。像这样使用div.replaceWith(“);
但是在这种情况下,你的句子中会有”
你需要转义它,如果“desc”中有引号,这将失败。如果“desc”中有引号,这将失败。