在 jQuery ReplaceWith() 函数中" />

使用
在 jQuery ReplaceWith() 函数中

发布于 2024-09-17 15:24:13 字数 988 浏览 4 评论 0 原文

我正在处理一个 PHP 文档,其中包含 jQuery 代码。我正在使用replaceWith() 函数来去除外部div 并替换为span。看来我只能用 替换它,而不是

这是为什么?

编辑:这是代码示例:

谢谢戴夫,但我实际上在上面的代码中使用了第二个选项,这就是我想要做的:

$response[] = "jQuery('#myID').replaceWith('<span>'+jQuery('#myID').html()+'</span>');";

上面的代码有效。基本上,我有一个 元素,我想用 元素替换它,并更改 ajax 响应上的类。

现在,如果我将类添加到上面的任何跨度:,就像这样:

$response[] = "jQuery('#myID').replaceWith('<span class"something">'+jQuery('#myID').html()+'</span>');";

代码中断,是因为 jQuery 语句上的外部双引号吗?

编辑

所以事实证明,除了转义类周围的双引号之外,我试图替换的 元素也存在问题:)。感谢您非常及时的帮助。我正在注册以在信用到期时给予信用:)

$response[] = "jQuery('#myID').replaceWith('<span class=\"something\">'+jQuery('#myID').html()+'</span>');";

I am working on a PHP document which has jQuery code in it. I am using a replaceWith() functions to strip outer divs and replace with span. it seems I can only replace it with <span> and not <span class="something">

Why is that?

Edit: Here is an example of the code:

Thanks Dave, but I actually use the second option in your code above, here is what I am trying to do:

$response[] = "jQuery('#myID').replaceWith('<span>'+jQuery('#myID').html()+'</span>');";

The above code works. Basically, I have an <a> element I want to replace with a <span> element and change the class on an ajax response.

Now, if I add classes to any of the spans above:, like this:

$response[] = "jQuery('#myID').replaceWith('<span class"something">'+jQuery('#myID').html()+'</span>');";

the code breaks, is it because of the outer double-quotes on the jQuery statement?

Edit

So it turned out to be an issue with my <a> element I was trying to replace, in addition to escaping the double-quotes around the class :). Thanks for the extremely prompt help. I am registering to give credit where credit is due :)

$response[] = "jQuery('#myID').replaceWith('<span class=\"something\">'+jQuery('#myID').html()+'</span>');";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2024-09-24 15:25:13

为了便于阅读,并避免像这样让自己感到困惑,请考虑使用以下语法进行 jQuery DOM 插入:

var myDiv = jQuery('#myID');
$('<span></span>').html(myDiv.html()).addClass('something').replaceAll(myDiv);

哦,如果您仔细查看上面问题中突出显示代码的方式,您已经可以看到您的内容了。我做错了;)

For the sake of readability, and to avoid confusing yourself like this, consider using this syntax for jQuery DOM insertion instead:

var myDiv = jQuery('#myID');
$('<span></span>').html(myDiv.html()).addClass('something').replaceAll(myDiv);

Oh, and if you look carefully at the way your code is highlighted in the question above, you can already see what you're doing wrong ;)

够运 2024-09-24 15:24:56

由于最大的变化是添加了引号,请仔细检查 class 属性是否未留下未终止的字符串:

// Broken:                 
$("div").replaceWith("<span class="something">");

// Fixed:
$('div').replaceWith('<span class="something">');

Since the biggest change there is the addition of quotes, double check that the class attribute isn't leaving an unterminated string open:

// Broken:                 
$("div").replaceWith("<span class="something">");

// Fixed:
$('div').replaceWith('<span class="something">');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文