mouseover 事件可以用在任何元素上,还是只能用在图像上?
<html >
<head>
<title>JavaScript Example</title>
<script type="text/javascript">
function greet {
var greet = document.getElementById("greeting");
greet.value="this is dynamic";
</script>
</head>
<body>
<p onmouseover="greet()"> Hello! Welcome to My Page </p>
</html>
这段代码有什么问题?
<html >
<head>
<title>JavaScript Example</title>
<script type="text/javascript">
function greet {
var greet = document.getElementById("greeting");
greet.value="this is dynamic";
</script>
</head>
<body>
<p onmouseover="greet()"> Hello! Welcome to My Page </p>
</html>
What is the problem in this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您尚未关闭
greet
函数(缺少结束}
字符)。其次,您在函数名称后面缺少括号:其次,您使用
getElementById
尝试获取对p
元素的引用,但是 < code>p 元素没有id
。第三,
greet
变量将包含对p
元素的引用,该元素没有value
属性(例如 < code>input 元素即可)。如果您试图更改元素的内容,您可能指的是innerHTML
。最后,您还没有关闭
元素。 编辑(参见评论) - 这不是问题,但我个人更喜欢关闭它以保持一致性。
您可以在调用函数时将对元素的引用传递给函数,这样您就不必通过 id 来获取它:
和 JavaScript:
Firstly, you haven't closed your
greet
function (missing the closing}
character). Secondly, you're missing the parentheses after the name of the function:Secondly, you're using
getElementById
to try and obtain a reference to thep
element, but thep
element doesn't have anid
.Thirdly, the
greet
variable will contain a reference to ap
element, which doesn't have avalue
property (like, for example,input
elements do). You may have meantinnerHTML
if you are trying to change the contents of the element.Finally, you haven't closed your
<body>
element. Edit (see comments) - This isn't a problem, but personally I prefer closing it for consistency.You could pass a reference to the element into the function when it's called, to save you having to get it by
id
:And JavaScript:
p 元素应该有一个 ID 为greet,如:
,这样当您选择元素的 ID 为:时,
文档可以找到您尝试从 HTML 文档中选择的标签。
此外,我认为您不需要编辑节点的“value”属性,而是需要使用“innerHTML”。所以这给出了:
我不太熟悉 JavaScript,但我相信这应该可行。
the p element should have an ID of greet, as in:
, so that when you select the element's ID at:
the document can find the tag you are trying to select from the HTML document.
Additionally, instead of editing the node's "value" attribute, I think you need to use the "innerHTML" instead. So that gives:
I am not exactly familiar with JavaScript, but I believe that should work.
您可以尝试:
或者
当然还有其他提示(例如相关
元素的
id
属性以及良好的 HTML 格式)。You can try:
or
Along with the other tips of course(like
id
attribute for the relevant<p>
element and well-forming your HTML).