我在textarea中有这个内容,我想用jquery删除PublisherImprintName标签
<PublisherInfo>
<PublisherName>Ask question</PublisherName>
<PublisherLocation>ph</PublisherLocation>
<PublisherImprintName>ask stackoverflow</PublisherImprintName>
<PublisherURL>stackoverflow.com</PublisherURL>
</PublisherInfo>
和可能的输出是
<PublisherInfo>
<PublisherName>Ask question</PublisherName>
<PublisherLocation>ph</PublisherLocation>
<PublisherURL>stackoverflow.com</PublisherURL>
</PublisherInfo>
是否可以使用jquery?
我搜索了一下,然后我发现了这段代码,但是我不能将它应用到我的问题中,但解决方案几乎相似
<script>
$(document).ready(function(){
$("button").click(function(){
$("PublisherImprintName").remove();
});
});
</script>
谢谢你。很抱歉提出太多问题
另一个问题:
如何删除标记名称但保留其值?
我在这里找到了一个解决方案: 我尝试将它应用于我的问题,但我无法使其正常工作
Remove a HTML tag but keep the innerHtml
这是我文字区域的文字
<PublisherInfo>
<PublisherName>Ask question</PublisherName>
<PublisherLocation>ph</PublisherLocation>
<PublisherImprintName>ask stackoverflow</PublisherImprintName>
<PublisherURL>stackoverflow.com</PublisherURL>
</PublisherInfo>
和可能的输出
<PublisherName>Ask question</PublisherName>
<PublisherLocation>ph</PublisherLocation>
<PublisherImprintName>ask stackoverflow</PublisherImprintName>
<PublisherURL>stackoverflow.com</PublisherURL>
答案 0 :(得分:2)
试试这个
var value = $("textarea").val();
value = value.replace(/<PublisherImprintName( |>).*?<\/PublisherImprintName>/gi,"");
$("textarea").val( value );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>
<PublisherInfo>
<PublisherName>Ask question</PublisherName>
<PublisherLocation>ph</PublisherLocation>
<PublisherImprintName>ask stackoverflow</PublisherImprintName>
<PublisherURL>stackoverflow.com</PublisherURL>
</PublisherInfo>
</textarea>
答案 1 :(得分:1)
你可以这样做,
$(function() {
function removeNode(str, nodeName) {
var pattern = '<'+nodeName+'>[\\s\\w]+<\/'+nodeName+'>';
var regex = new RegExp(pattern, 'gi');
return str.replace(regex, '').replace(/^\s*[\r\n]/gm, '');
}
$("#myBtn").on('click', function () {
var txt = $("#txtArea").text();
var newText = removeNode(txt, 'PublisherImprintName');
$('#txtArea').text(newText);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="txtArea" rows="10" cols="50">
<PublisherInfo>
<PublisherName>Ask question</PublisherName>
<PublisherLocation>ph</PublisherLocation>
<PublisherImprintName>ask stackoverflow</PublisherImprintName>
<PublisherURL>stackoverflow.com</PublisherURL>
</PublisherInfo>
</textarea>
<button id="myBtn">Button</button>
修改强>
对于您的另一个问题,您可以这样做..
$(function() {
function removeNodeButRetain(str, nodeName) {
var pattern = '<\/?'+nodeName+'>';
var regex = new RegExp(pattern, 'gi');
return str.replace(regex, '').replace(/^\s*[\r\n]/gm, '');
}
$("#myBtn").on('click', function() {
var txt = $("#txtArea").text();
var newText = removeNodeButRetain(txt, 'PublisherInfo');
$('#txtArea').text(newText);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="txtArea" rows="10" cols="50">
<PublisherInfo>
<PublisherName>Ask question</PublisherName>
<PublisherLocation>ph</PublisherLocation>
<PublisherImprintName>ask stackoverflow</PublisherImprintName>
<PublisherURL>stackoverflow.com</PublisherURL>
</PublisherInfo>
</textarea>
<button id="myBtn">Button</button>
答案 2 :(得分:0)
jquery中的搜索属性。使用jquery的属性功能你可以删除它 https://api.jquery.com/attribute-contains-selector/