gpt4 book ai didi

javascript - 使用jquery修改textarea文本

转载 作者:行者123 更新时间:2023-11-30 11:54:28 24 4
gpt4 key购买 nike

美好的一天。我如何使用javascript修改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>

最佳答案

试试这个

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>

关于javascript - 使用jquery修改textarea文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38429157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com