mouseover 事件可以用在任何元素上,还是只能用在图像上?

发布于 2024-12-16 22:14:34 字数 421 浏览 0 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若相惜即相离 2024-12-23 22:14:34

首先,您尚未关闭 greet 函数(缺少结束 } 字符)。其次,您在函数名称后面缺少括号:

function greet() {
    //Function body
}

其次,您使用 getElementById 尝试获取对 p 元素的引用,但是 < code>p 元素没有 id

第三,greet 变量将包含对 p 元素的引用,该元素没有 value 属性(例如 < code>input 元素即可)。如果您试图更改元素的内容,您可能指的是innerHTML

最后,您还没有关闭 元素。 编辑(参见评论) - 这不是问题,但我个人更喜欢关闭它以保持一致性。

您可以在调用函数时将对元素的引用传递给函数,这样您就不必通过 id 来获取它:

<p onmouseover="greet(this);">Example</p>

和 JavaScript:

function greet(elem) {
    elem.innerHTML = "Something new";
}

Firstly, you haven't closed your greet function (missing the closing } character). Secondly, you're missing the parentheses after the name of the function:

function greet() {
    //Function body
}

Secondly, you're using getElementById to try and obtain a reference to the p element, but the p element doesn't have an id.

Thirdly, the greet variable will contain a reference to a p element, which doesn't have a value property (like, for example, input elements do). You may have meant innerHTML 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:

<p onmouseover="greet(this);">Example</p>

And JavaScript:

function greet(elem) {
    elem.innerHTML = "Something new";
}
∝单色的世界 2024-12-23 22:14:34

p 元素应该有一个 ID 为greet,如:

<p onmouseover="greet()" id="greeting"> Hello! Welcome to My Page </p> 

,这样当您选择元素的 ID 为:时,

document.getElementById("greeting");

文档可以找到您尝试从 HTML 文档中选择的标签。

此外,我认为您不需要编辑节点的“value”属性,而是需要使用“innerHTML”。所以这给出了:

<html >
 <head>   
  <title>JavaScript Example</title>
    <script type="text/javascript">
      function greet { 
        var greet = document.getElementById("greeting");
        greet.innerHTML="this is dynamic";
        }
    </script>
 </head>
 </body>
   <p onmouseover="greet()" id="greeting"> Hello! Welcome to My Page </p> 
</html> 

我不太熟悉 JavaScript,但我相信这应该可行。

the p element should have an ID of greet, as in:

<p onmouseover="greet()" id="greeting"> Hello! Welcome to My Page </p> 

, so that when you select the element's ID at:

document.getElementById("greeting");

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:

<html >
 <head>   
  <title>JavaScript Example</title>
    <script type="text/javascript">
      function greet { 
        var greet = document.getElementById("greeting");
        greet.innerHTML="this is dynamic";
        }
    </script>
 </head>
 </body>
   <p onmouseover="greet()" id="greeting"> Hello! Welcome to My Page </p> 
</html> 

I am not exactly familiar with JavaScript, but I believe that should work.

久而酒知 2024-12-23 22:14:34

您可以尝试:

greet = function(elem) {
             elem.innerHTML = "Something new";
        }

或者

greet = function() {
            var greet = document.getElementById("greeting");
            greet.innerHTML="this is dynamic";
        }

当然还有其他提示(例如相关

元素的 id 属性以及良好的 HTML 格式)。

You can try:

greet = function(elem) {
             elem.innerHTML = "Something new";
        }

or

greet = function() {
            var greet = document.getElementById("greeting");
            greet.innerHTML="this is dynamic";
        }

Along with the other tips of course(like id attribute for the relevant <p> element and well-forming your HTML).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文