Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 语法错误无法识别的表达式将html标记包装为字符串_Javascript_Jquery_Html - Fatal编程技术网

Javascript 语法错误无法识别的表达式将html标记包装为字符串

Javascript 语法错误无法识别的表达式将html标记包装为字符串,javascript,jquery,html,Javascript,Jquery,Html,这里怎么了 var str = 'Text coming from the caption of this image. The caption describes the photo and can be short, or very long. This can keep going on multiple lines but should be truncated if too long otherwise the summary window will grow very big'; $

这里怎么了

var str = 'Text coming from the caption of this image. The caption describes the photo and can be short, or very long. This can keep going on multiple lines but should be truncated if too long otherwise the summary window will grow very big';
$(str).wrap('<div></div>');
var str='来自此图像标题的文本。标题描述了照片,可以很短,也可以很长。这可以在多行上继续进行,但如果太长,则应将其截断,否则摘要窗口将变得非常大';
$(str).wrap(“”);
这在我看来是合法的。绳子有什么问题吗


您在
str
上调用jQuery,它只是一个普通字符串。使用
$(str)
时,请尝试使用类似于
div
span
的HTML元素将其包围,
str
必须是选择器或HTML字符串。在您的例子中,由于它不是以HTML标记开头,所以假定它是一个选择器。但它只是一些随机文本,当它试图将其作为选择器进行解析时,会出现错误。问题是字符串中的单词
图像。
不是有效的选择器,因为它在单词末尾有
,并且
后面必须跟一个类名

要将文本放入
div
,请将其用作新
元素的
文本:

$('<div>', { text: str })
$('',{text:str})

包装字符串没有问题,尝试将var str值更改为“hello”啊,你是对的。它好像被
卡住了。可能试图将其解释为类选择器。@NatelyJamerson我在上面的评论中解释过,你为什么不明白?@Barmar不,choking的意思是?@NatelyJamerson将
$('div')
的结果与
$('div'))
进行比较。第一种方法有效,第二种方法由于单词末尾的
而出错错误消失了?这是一个有效的选择器,它将匹配一个元素,如
。尝试查看我创建的这个示例来解释我的问题,它就像执行
$('hello')
——它是一个选择器,用于查找该标记名。它不返回任何内容,因为DOM中没有
元素。但这不再是语法错误。尝试执行
$('image.')
,您将得到错误。选择器不能将
放在单词的末尾,它必须放在单词的开头,因为它意味着用该类选择元素。