javascript - 语法错误 unrecognized expression wrap html tag to string

标签 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';
$(str).wrap('<div></div>');

这对我来说似乎是合法的。字符串有什么问题吗?

https://jsbin.com/lecuregede/1/edit?html,js,console

最佳答案

当您使用 $(str) 时, str必须是选择器或 HTML 字符串。在您的情况下,由于它不是以 HTML 标记开头,因此假定它是一个选择器。但它只是一些随机文本,当它试图将其解析为选择器时会出错。问题是image.这个词在字符串中,这不是一个有效的选择器,因为它有 .在单词的末尾,和 .必须后跟类名。

将文本放入 div , 将其用作 text一个新的 <div>元素:

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

关于javascript - 语法错误 unrecognized expression wrap html tag to string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41626868/

相关文章:

javascript - 如何从 react/redux 中的加载 Action 触发加载动画

javascript - IndexedDB 使用键获取所有数据

jquery - 高级 jQuery "Sticky Bottom"以及滚动侧边栏问题

javascript - 使用搜索词过滤树结构 json 的 Javascript 函数。排除与搜索词不匹配的任何对象

javascript - 内联 javascript 中的重音符号

javascript - 解除绑定(bind) jquery 事件仅有效一次

javascript - 每次重绘Google Chart时都会创建新的containerTable

html - 如何在具有 contentEditable 的容器中制作不可编辑元素?

javascript - 在 AngularJS 中输出键值对作为链接

html - 如何在一个字体文件中包含不同的字体粗细?