gpt4 book ai didi

Javascript - 从元素中删除句子(不包含 id/class),但保留其余内容

转载 作者:行者123 更新时间:2023-12-03 02:04:39 25 4
gpt4 key购买 nike

我正在尝试制作一个greasemonkey 脚本,我将在一个网站上使用该脚本来填写表格。我不拥有该网站。主要目的是消除“信息文本”的“困惑”,只留下输入/选择字段。

这里的问题是该段落没有附加类或 id。它确实有一个我想删除的句子,但段落也有一个输入以及同一段落内的该句子。我希望保留 INPUT 但删除句子。

网站上的表单有多个段落具有相同的“问题”,因此并不是每一份表单都有一个“问题”。不同段落的句子也不同。这是它的简化版本:

<div id="shop_info">
<p>
32. Enter the store's name/logo
<input id="store_name" type="text" name="store_name">
</p>

<p>
33. Enter the store's phone number
<input id="store_phone" type="text" name="store_phone">
</p>
</div>

^ 在上面的示例中,我需要删除这些句子:
32.输入商店名称/ Logo
33.输入商店电话号码

因此,脚本完成工作后,它应该如下所示:

<div id="shop_info">
<p>
<input id="store_name" type="text" name="store_name">
</p>

<p>
<input id="store_phone" type="text" name="store_phone">
</p>
</div>

...如果有办法从段落中获取输入(现在不需要),最好能像这样结束:

<div id="shop_info">
<input id="store_name" type="text" name="store_name">

<input id="store_phone" type="text" name="store_phone">
</div>

...但即使使用第一个解决方案,我也会很高兴(如果输入保留在段落内,我只需要这些句子消失)。

我环顾四周,但我找到的代码示例不起作用,因为大多数解决方案“搜索”单个文本单词而不是整个句子。我只粘贴了该页面的简化代码。还有很多段落中嵌套有 INPUT。

有人知道如何解决这个问题吗?

提前致谢!

编辑:
我检查了你们中的一些人提供的一些代码。由于某种原因,即使它可以在 codepen 或其他“脚本测试”站点上运行,但一旦我在greasemonkey 脚本中实现它,代码就无法运行。我决定向您展示该表单在网站本身上的样子。这是一个针对微型 worker 的亚马逊网站。这是带有表单本身的页面:

page in question at Amzn microworkers website

这是包含 3 张图像的 imgur 画廊:

imgur gallery with 3 images

第一张图片:整个页面的样子
第二张图片:到目前为止我得到的
第三张图片:我想要实现的目标(这就是我创建这个问题的原因)

我希望我现在已经让你的生活变得更轻松了,这样你就可以(如果你愿意)直接在网页本身测试代码。

再次感谢您的帮助!

最佳答案

此片段可能有用,已添加注释来描述步骤

//get the parent element by the id
let getElem = document.getElementById("shop_info");
// get all the paragraph using querySelectorAll &
//iterate over it using forEach;
getElem.querySelectorAll('p').forEach(function(item) {
// get the input element, this will be appended later
let getInput = item.querySelector('input');
// empty the p element
item.innerHTML = "";
// append the input back to the paragraph
item.appendChild(getInput);
})
<div id="shop_info">
<p>
32. Enter the store's name/logo
<input id="store_name" type="text" name="store_name">
</p>

<p>
33. Enter the store's phone number
<input id="store_phone" type="text" name="store_phone">
</p>
</div>

关于Javascript - 从元素中删除句子(不包含 id/class),但保留其余内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49849024/

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