在html中调用变量
我正在尝试制作一个脚本,当您输入十六进制值并按提交时,它将文本颜色更改为输入的颜色。
看来问题是我在变量 new html 中调用变量“userInput”的方式
有什么想法吗?
<script type="text/javascript">
function changeText3(){
var userInput = document.getElementById('userInput').value;
var oldHTML = document.getElementById('para').innerHTML;
var newHTML = "<span style='color:userInput'>" + oldHTML + "</span>";
document.getElementById('para').innerHTML = newHTML;
}
</script>
<p id='para'>Welcome to the site <b>dude</b> </p>
<input type='text' id='userInput' value='#' />
<input type='button' onclick='changeText3()' value='Change Text'/>
I am trying to make a script that when you type in a hex value and press submit itchanges the text color to the color inputted.
It seems the problem is the way i am calling the variable "userInput" inside the variable new html
Any Ideas?
<script type="text/javascript">
function changeText3(){
var userInput = document.getElementById('userInput').value;
var oldHTML = document.getElementById('para').innerHTML;
var newHTML = "<span style='color:userInput'>" + oldHTML + "</span>";
document.getElementById('para').innerHTML = newHTML;
}
</script>
<p id='para'>Welcome to the site <b>dude</b> </p>
<input type='text' id='userInput' value='#' />
<input type='button' onclick='changeText3()' value='Change Text'/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用样式参考而不是添加越来越多的跨度:
I would suggest to use the style reference instead of adding more and more spans:
这只是导致问题的一行:
It's just the one line that's causing the problem:
您需要将 userInput 变量“注入”到 newHTML 变量中。见下文;
you need to "inject" the userInput variable into your newHTML variable. See below;