如何使用jQuery/普通的旧javascript更改元素(例如h1-> h2)? [英] How do I change an element (e.g. h1 -> h2) using jQuery / plain old javascript?

查看:64
本文介绍了如何使用jQuery/普通的旧javascript更改元素(例如h1-> h2)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然,存在与文档有效性有关的所有可能的错误,但是当将段落(p)更改为address元素时,我的绊脚石发生了.我当前的方法是(或多或少):

Of course, there are a whole range of possible errors relating to document validity, but my immediate stumbling block occurs when changing a paragraph (p) into an address element. My current method is (more-or-less):

var p = $('p#test');
p.replaceWith('<address>' + p.html() + '</address>');

,但在此特定情况下失败;它非常适合p-> blockquote或h2-> h3. Firebug建议出于某种原因将自动关闭元素(<address/>)添加到文档中.

but that fails for this specific case; it works perfectly for p -> blockquote or h2 -> h3. Firebug suggests that a self-closing element (<address/>) has been added to the document, for some reason.

任何人都可以发现该错误或提出替代方法吗?

Can anyone spot the bug or suggest an alternative method?

推荐答案

var p = $('p#test');
var a = $('<address></address>').
    append(p.contents());
p.replaceWith(a);

您的解决方案会遇到各种可怕的HTML转义问题以及可能的注入攻击.

Your solution is subject to all sorts of horrible HTML escaping issues and possibly injection attacks.

这篇关于如何使用jQuery/普通的旧javascript更改元素(例如h1-> h2)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆