如何使用javascript将p标签替换为br
我该如何替换这个:
<p class='abc'>blah blah</p> afdads
用这个:
blah blah <br/>
使用(普通?)JavaScript?
谢谢!
更新内容
$content = $_REQUEST['content'];
$content = preg_replace('/<p[^>]*>/', '', $content); // Remove the start <p> or <p attr="">
$content = preg_replace('/</p>/', '<br />', $content); // Replace the end
echo $content; // Output content without P tags
我想要这样的东西......希望这次你们能明白我的问题。
谢谢
How would I replace this:
<p class='abc'>blah blah</p> afdads
With this:
blah blah <br/>
Using (vanilla?) JavaScript?
Thanks!
Updated Content
$content = $_REQUEST['content'];
$content = preg_replace('/<p[^>]*>/', '', $content); // Remove the start <p> or <p attr="">
$content = preg_replace('/</p>/', '<br />', $content); // Replace the end
echo $content; // Output content without P tags
I would something like this...hope this time you mates have got my question.
Thankyou
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢您提出这个问题,您是救星,GitsD。
我将你的 php 函数替换为 javascript,它成功了!
内容现在将用 '' 替换 p 标签,用 br 替换 /p 标签
Thank you for posting this question, you're a lifesaver, GitsD.
I replaced your php function to javascript and it worked!
content would now replaced p tags with '' and /p tags with br
你可以做(如果你谈论字符串)
You could do (if you talk about strings)
恐怕你的问题没有明确说明。假设您想对该类的所有 p 标签执行此操作,并且您正在 Web 浏览器环境中工作:
I'm afraid your question isn't well-specified. Assuming you want to do that to all p tags with that class, and you're working in a web browser environment:
如果您实际上指的是 JavaScript 而不是 PHP,则可以这样做:这
是有效的,因为
getElementsByTagName
返回的NodeList
是“live”,这意味着当我们从文档中删除p
节点时,它也会从列表中删除。If you actually meant JavaScript and not PHP, this would do it:
This works because the
NodeList
returned bygetElementsByTagName
is ‘live’, meaning that when we remove ap
node from the document, it also gets removed from the list.