使用JavaScript更改HTML中标签之间的内容

huangapple go评论71阅读模式
英文:

change content between tags in html using javascript

问题

在HTML中,使用textContent属性无法更改链接(a标签)的内容,以下是您提供的代码示例:

第一个HTML页面中的代码:
```html
<li><a name="regbutton" href="loginReg.html">Register</a></li>

第二个HTML页面中的代码:

<button type="button" id="regDirect" onclick="register()">点击注册</button>

您想要将"Register"更改为一个名称值。这是JavaScript函数:

function register() {
  let name = document.getElementById("RegisterForm").value; // 从第二个HTML页面的表单中获取名称
  document.getElementsByName("regbutton")[0].textContent = name; 
  window.location.href = "index.html"; // 重定向到第一个页面
}

但是,如果JavaScript中包含以下这行代码:

document.getElementsByName("regbutton")[0].textContent = name;

那么按钮(id = regDirect)将不起作用。如果删除此行,重定向会起作用,但值("Register")不会更改。


<details>
<summary>英文:</summary>

**cant change a content using textContent property in js for a (a) tag in html**


so i have this code in my first html page

<li><a name="regbutton" href="loginReg.html">Register</a></li>


and this code in my second html page

<button type="button" id="regDirect" onclick="register()">


so i wanted to change &quot;register&quot; to a name value , Here is Js function

function register() {
let name = document.getElementById("RegisterForm").value; //get name from form in second html page
document.getElementsByName("regbutton").textContent = name;
window.location.href = "index.html"; // redirect to first page
}


but the button ( id = regDirect ) dont work when this line in JavaScript is present

document.getElementsByName("regbutton").textContent = name;


if i deleted this line the redirect works but value ( register ) doesnt change


</details>


# 答案1
**得分**: 1

简单来说,你将无法使用这些工具完成你设定的任务。你只能使用 JavaScript 访问当前加载的页面。当然,这并不完全正确,但你不能像这样随意更改完全不同的页面,你明白我的意思。

然而,你可以像你打算的那样动态地更改文本。你可以,例如,通过 URL 传递数据,然后使用 JavaScript 访问数据。

在重定向用户的位置,你可以将 `index.html` 替换为例如 `index.html?buttontext=hello`。

当完成后,你可以在需要动态更改文本的页面上进行编辑。你可以使用以下代码获取参数:
```javascript
let params = new URL(document.location).searchParams;
let buttontext = params.get("buttontext");

你现在可以获取你的文本了,但你更改文本的方式也是不正确的。document.getElementsByName 返回一个 NodeList。然而,你可以访问这个 NodeList 的第一个元素,这很可能是你的按钮。因此,你可以使用以下代码设置文本:

document.getElementsByName("regbutton")[0].textContent = buttontext

编辑

以下是一些包含这些元素的工作示例:

1.html

<ul>
  <li>
    <a name="regbutton" href="2.html">Register</a>
  </li>
</ul>

<script>
  document.getElementsByName("regbutton")[0].textContent = new URL(document.location).searchParams.get("buttontext");
</script>

2.html

<input id="RegisterForm">
<button type="button" id="regDirect" onclick="register()">Go</button>

<script>
function register() {
  let name = document.getElementById("RegisterForm").value; // 从表单中获取名称
  window.location.href = "1.html?buttontext=" + name; // 重定向到第一个页面
}
</script>

请注意,1.html 在这里不会产生有价值的东西,你需要制作文件的本地副本。

英文:

To put it simply, you won't be able to do what you set out to do with these tools. You can only access the currently loaded page with JavaScript. That - of course - isn't entirely true but you can't change a totally different page just like that, you get the point.

You can, however, change the text dynamically like you intend. You could - for example - pass the data in the URL and then access the data with JavaScript.

Where you redirect the user you can replace index.html with for example index.html?buttontext=hello.

When that's done, you can edit your page where you want the text to change dynamically. You can fetch the parameters with:

let params = new URL(document.location).searchParams;
let buttontext = params.get(&quot;buttontext&quot;);

You can snatch your text now, but your way of setting the text is also incorrect. document.getElementsByName returns a NodeList. You can access this NodeList's first element though, that is most likely your button. Therefore you can set the text with:

document.getElementsByName(&quot;regbutton&quot;)[0].textContent = buttontext

Edit

Here are some working snippets incorporating these elements:

1.html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;ul&gt;
  &lt;li&gt;
    &lt;a name=&quot;regbutton&quot; href=&quot;2.html&quot;&gt;Register&lt;/a&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;script&gt;
  document.getElementsByName(&quot;regbutton&quot;)[0].textContent = new URL(document.location).searchParams.get(&quot;buttontext&quot;);
&lt;/script&gt;

<!-- end snippet -->

2.html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;input id=&quot;RegisterForm&quot;&gt;
&lt;button type=&quot;button&quot; id=&quot;regDirect&quot; onclick=&quot;register()&quot;&gt;Go&lt;/button&gt;

&lt;script&gt;
function register() {
  let name = document.getElementById(&quot;RegisterForm&quot;).value; // get name from form
  window.location.href = &quot;1.html?buttontext=&quot;+name; // redirect to first page
}
&lt;/script&gt;

<!-- end snippet -->

Keep in mind that 1.html won't produce anything of value here, you'll have to make a local copy of the files.

huangapple
  • 本文由 发表于 2023年6月2日 01:56:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定