.replacement()无法删除<strong>标签
.replace() not working to remove <strong> tags
Hi我正在使用jquery获取div的html内容,删除强标记,然后用新内容更新div。但由于某种原因,它不起作用。这是我的代码:
var content = $('#mydivid').html();
content = content.replace('<strong>', '').replace('</strong>', '');
$('#mydivid').html(content);
有人知道为什么不起作用吗?
首先放入警报以检查内容。。。
alert(content);
如果有效的话,我会def不使用replaceWith,试试。。。
$('#mydivid').html(content);
第一个:
您不需要替换两次,replace((的第一个参数是regex,因此您可以:
content.replace(/<'/?strong>/g, "");
以移除所有的CCD_ 1和CCD_。
第二:
replaceWith()
不是你想要的,html()
是你的目标。
这就是你想要的:
$(function() {
$("#mydivid").html(function() {
return $(this).html().replace(/<'/?strong>/g, "");
});
});
jsfiddle:http://jsfiddle.net/pS5Xp/
我能看到的代码的唯一问题是替换div iteself。你可能想这样做:-
var content = $('#mydivid').html();
content = content.replace('<strong>', '').replace('</strong>', '');
$('#mydivid').html(content);
还要确保你有小写的强标签。
样品:http://jsfiddle.net/8pVdw/
var content = $('#mydivid').html();
$('#mydivid').html(content.replace('<strong>', '').replace('</strong>', ''));
应该有效。
如果没有,只需提醒内容,看看它是否有效。
您可以尝试:
var div = $('#mydivid');
var strong = div.find("strong");
strong.replaceWith(strong.text());
这只需要找到<strong>
标记并将其替换为文本内容。小提琴在这里:http://jsfiddle.net/cWpUK/1/
/**
* Syntax:
* Node replaceNode(Node newNode, Node oldNode)
* Node replaceNode(String newNode, Node oldNode)
* Node replaceNode(Object newNode, Node oldNode)
* - newNode: { String localName [, String namespaceURI ] }
* null replaceNode(null newNode, Node oldNode)
* - will delete the old node and move all childNodes to the parentNode
**/
// nodes from another document has to be imported first
// should throw an Error if newNode is descendant-or-self
// type of newNode is tested by 'firstChild' (DOM 1)
// returns new Node
var replaceNode = (function() {
var replaceNode = function(newNode, oldNode) {
var document = oldNode.ownerDocument;
if(newNode === null)
newNode = document.createDocumentFragment();
else if(typeof newNode === 'string' || newNode instanceof String)
newNode = {localName: newNode};
if(!('firstChild' in newNode)) {
var namespaceURI = 'namespaceURI' in newNode ?
newNode.namespaceURI : replaceNode.namespaceURI;
newNode = namespaceURI === null ?
document.createElement(newNode.localName) :
document.createElementNS(namespaceURI, newNode.localName);
}
var parentNode = oldNode.parentNode,
nextSibling = oldNode.nextSibling;
parentNode.removeChild(oldNode);
if(newNode.nodeType === 1 || newNode.nodeType === 11)
while(oldNode.firstChild)
newNode.appendChild(oldNode.firstChild);
parentNode.insertBefore(newNode, nextSibling);
return newNode.nodeType === 11 ? null : newNode;
};
replaceNode.namespaceURI = null;
return replaceNode;
})();
这将用另一个节点替换一个节点。这样做的优点是不会破坏后续节点上的EventListener。
var nodeList = document.getElementById('mydivid').getElementsByTagName('strong');
while(nodeList.length)
replaceNode(null, nodeList[ nodeList.length - 1 ]);
测试用例
您可能不想使用replaceWith。您应该通过再次调用.html((来用新内容更新div:
var content = $('#mydivid').html();
content = content.replace('<strong>', '').replace('</strong>', '');
$('#mydivid').html(content);
测试时效果良好-http://jsbin.com/ihokav/edit#preview
一个更好的选择是在强标签上使用replaceWith:
$('#mydivid strong').replaceWith(function() { return $(this).html(); });
http://jsbin.com/ihokav/2/edit
相关文章:
- 在<页眉>标签
- 如何更改<svg>标记为<img>用js标记
- 如何逃离<>在javascript下划线模板中
- 有没有一种方法可以检测ios<>使用jquery和触发器操作形成导航按钮
- 正则表达式,它允许除“”之外的所有字符<>_;{}[]”;
- ajax调用:响应<->数据类型
- 如果所有三个文本框<>然后是100%
- CORS的问题.烧瓶<->AngularJS
- 有没有一种方法可以创建与Java<->JSON映射对象,如JSON.stringfy(jsObject)创建的
- 什么是%<>%调用了语法
- JavaScript返回值:What dos<>意思是
- JavaScript中哪种类型的值对应于Java`List<>`
- 如何发送类似“<>"在参数中使用$.ajax-to-server
- 不等于<>操作员不工作
- jquery附加函数转换<>到实体名称
- 只有当超过3个元素时才显示更多内容按钮,否则不显示.使用jquery's:lt:gt
- 为什么在<>ckeditor中不支持
- url有效性的Java脚本regexp,带有<>
- 设计Javascript前端<->C++后端通信
- 比较查询运算符ObjectId<->日期