Javascript JS函数调用
我从JS开始,在尝试编写一个特定函数时遇到了问题。目的是在按下按钮后更改文本。“没什么”的文字应该变成“你说嗨,我说嗨?”和“嗨?”?文档中已存在文本 这是代码,欢迎任何提示Javascript JS函数调用,javascript,Javascript,我从JS开始,在尝试编写一个特定函数时遇到了问题。目的是在按下按钮后更改文本。“没什么”的文字应该变成“你说嗨,我说嗨?”和“嗨?”?文档中已存在文本 这是代码,欢迎任何提示 <html> <head> <title>Javascript test </title> </head> <body> <p id="hey">Hey?</p> <p id ="nothing">Nothing
<html>
<head>
<title>Javascript test </title>
</head>
<body>
<p id="hey">Hey?</p>
<p id ="nothing">Nothing</p>
<INPUT type =button VALUE="Button?" OnClick=(take())>
<script>
function take(){
var hey=document.getElementById("hey");
var nothing =document.getElementById("one");
nothing.appendChild(document.createTextNode("You say hi, I say " + hey));
}
</script>
</body>
</html>
Javascript测试
嘿
没事
函数take(){
var hey=document.getElementById(“hey”);
var nothing=document.getElementById(“一”);
appendChild(document.createTextNode(“你说你好,我说”+hey));
}
好的,谢谢大家的帮助。没有人是100%正确的,但我尝试了所有建议的组合,并获得了99%的路,我想去的地方。我的代码现在看起来像这样。我试图做的最后一个更改不是追加新的文本字符串,而是用当前追加的字符串替换旧字符串
<html>
<head>
<title>Javascript test </title>
</head>
<body>
<p id="hey">Hey?</p>
<p id ="nothing">Nothing</p>
<input type ="button" value="Button?" OnClick="take();"/>
<script>
function take(){
var hey=document.getElementById("hey");
var nothing =document.getElementById("nothing");
nothing.appendChild(document.createTextNode("You say hi, I say " + hey.innerHTML));
}
</script>
</body>
</html>
Javascript测试
嘿
没事
函数take(){
var hey=document.getElementById(“hey”);
var nothing=document.getElementById(“nothing”);
appendChild(document.createTextNode(“你说你好,我说”+hey.innerHTML));
}
感谢大家的帮助,谢谢栈溢社区 将您的输入更改为此
<input type=button value="Button?" onclick="take();" />
Javascript测试
嘿
没事
函数take(){
var hey=document.getElementById(“hey”);
var nothing=document.getElementById(“nothing”);
appendChild(document.createTextNode(“你说你好,我说”+hey.value));
}
我已更改输入标记的结尾在输入类型和OnClick中添加引号
您正在执行
document.getElementById(“一”)
,但没有id为one
的元素。因此更改为nothing
您的代码有一些问题
首先,您应该在脚本标记上指定type=“text/javascript”
其次,您的输入应如下所示:
<input id="button" type="button" value="Button?" />
<INPUT type="button" VALUE="Button?" OnClick="take()">
var nothing = document.getElementById("nothing");
第四,函数查找id为1的元素,该元素不存在。你是说idnothing
第五,假设您使用正确的id来获取nothing
元素,那么在设置值时需要使用另一个p
的值
var take = function () {
var hey = document.getElementById('hey');
var nothing = document.getElementById('nothing');
nothing.innerHTML= 'You say hi, I say ' + hey.innerHTML;
};
请参见此处的工作示例:
作为补充说明,我发现了一个非常适合jQuery和本机javascript的学习网站。这里有几点提示: 将按钮输入html更改为如下所示:
<input id="button" type="button" value="Button?" />
<INPUT type="button" VALUE="Button?" OnClick="take()">
var nothing = document.getElementById("nothing");
要更改文本,只需使用以下代码即可:
nothing.innerText = "You say hi, I say " + hey.innerText;
所以你想在没什么后加上“你说你好,我说你好”吗?为什么要使用“hey”元素?您可能需要研究jQuery。这样做更容易。WOZ,我不认为这值得从jQuery的开销,但这只是梅梅喜欢“没有”文本变成“你说嗨,我说嘿”。我得到了Hey元素来获取文本变量,但我不知道如何将变量放入行中。。。appendChild(document.createTextNode(“你说你好,我说你好”);请解释您所做的修复,这样人们就不必做字符串比较来找到您所做的。这仍然是无效的html,没有结束输入标记,而且它应该是小写的
input
我找到了类似于html和CSS的JQFundamentals的教程,但找不到JS的。非常感谢!