如何根据用户输入使用 JavaScript 启动字幕?
当用户将他们的名字放入文本框中然后单击按钮时,我尝试使用 JavaScript 来启动选取框。我已经有了一个关于如何做到这一点的想法,但我的脚本从未完全起作用。任何帮助表示赞赏!
这是我到目前为止所拥有的:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function StartMarquee() {
var text = document.getElementById(namebox);
if (text != null) {
document.write("<marquee behavior='scroll' direction='right'>Hello " + text + "!</marquee>");
}
else {
alert("Enter your name first!!!");
}
}
</script>
</head>
<body>
<table style="margin:0px auto 0px auto;">
<tr><td>Enter your name!</td>
<td><input type="text" id="namebox"/></td>
<td><input type="button" value="Enter" onclick="StartMarquee()"/></td></tr>
</table>
</body>
</html>
I am trying to use JavaScript to start a marquee when a user puts their name into a textbox and then clicks a button. I've got an idea as to how to do it, but my script never fully works. Any help is appreciated!
Here's what I have so far:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function StartMarquee() {
var text = document.getElementById(namebox);
if (text != null) {
document.write("<marquee behavior='scroll' direction='right'>Hello " + text + "!</marquee>");
}
else {
alert("Enter your name first!!!");
}
}
</script>
</head>
<body>
<table style="margin:0px auto 0px auto;">
<tr><td>Enter your name!</td>
<td><input type="text" id="namebox"/></td>
<td><input type="button" value="Enter" onclick="StartMarquee()"/></td></tr>
</table>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 JavaScript 有一些问题。
namebox
传递给getElementById
。您需要将其放在引号中('namebox'
)。text
的值,而不是null
。text.value
而不是text
)。以下是经过这些修复后您的代码的样子:
其他一些一般建议:
document.write
。相反,使用 DOM 方法创建一个新元素并将其插入到 DOM 中。===
和!==
作为条件以避免类型强制并确保您得到您认为的结果。marquee
。Your JavaScript has a few problems.
namebox
togetElementById
. You need to put this in quotes ('namebox'
).text
against the empty string, notnull
.text.value
as opposed to justtext
) in the element you're creating.Here is what your code would look like with these fixes:
Some other general suggestions:
document.write
. Instead, use DOM methods to create a new element and insert it into the DOM.===
and!==
for conditions to avoid type coercion and to ensure you're getting the result you think you are.marquee
.您可能不想为此使用
document.write
- 使用document.createElement('marquee')
创建元素,然后将其添加到正文页面的。您可以在返回的元素上设置方向等属性,并将其innerHTML
设置为您想要在选取框中显示的文本。(PS字幕?真的吗?)
You probably don't want to use
document.write
for this-- usedocument.createElement('marquee')
to create the element, and then add it to the body of the page. You can set attributes like direction on the element you get back, and set itsinnerHTML
to the text you want in the marquee.(P.S. Marquee? Really?)
使用两个 html:第二个 html 中的一个框相当简单
通过 iframe 适合下一个 html 代码
关注 https://eniefiok.blogspot.com
use two html: one box in rather plain second html
fits into next html code by iframe
follow https://eniefiok.blogspot.com