英文:
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 "register" 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("buttontext");
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("regbutton")[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 -->
<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>
<!-- end snippet -->
2.html
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<input id="RegisterForm">
<button type="button" id="regDirect" onclick="register()">Go</button>
<script>
function register() {
let name = document.getElementById("RegisterForm").value; // get name from form
window.location.href = "1.html?buttontext="+name; // redirect to first page
}
</script>
<!-- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论