mouseover事件中的引用变量 [英] Reference variable in the mouseover event

查看:59
本文介绍了mouseover事件中的引用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在鼠标悬停事件中引用createElement变量.我试图显示一个在鼠标悬停事件后将具有onclick事件的按钮.

How can I reference a createElement variable in a mouseover event. I am trying to show a button that will have an onclick event after a mouseover event.

var lis = document.getElementsByTagName('li');
var element = document.createElement('input');

for (i=0; i<lis.length; i++){ 
   lis[i].onmouseover = function(){ 

	 element.type='Button';
	 element.name = 'Edit'
      // alert(event.clientX+':'+event.clientY);
 
	 this.style.backgroundColor = "red";
      // this.element();

    }; 
    lis[i]. önmouseout = function(){ 
      //alert('Mouse Out');
	this.style.backgroundColor = "white";

    }; 
} 




在此先感谢




Thanks in advance

推荐答案

您当然可以,因为document.createElement返回DOM元素.

唯一的问题是您的新创建的元素在文档的DOM中不存在:您没有在问题中显示的代码中追加或插入它.

请参阅说明如何创建元素并使用element.appendChild添加元素的文档页面(例如):
http://reference.sitepoint.com/javascript/Document/createElement [
Of course you can, because document.createElement returns DOM element.

The only problem is that your freshly created element does not exist in the DOM of your document: you did not append or insert it in the code you show in your question.

Please see the documentation page demonstrating how to create an element and add it using element.appendChild (for example):
http://reference.sitepoint.com/javascript/Document/createElement[^].



Of course, to handle an event by the freshly created element, use the variable immediately:

element.onmouseover = function() { /* ... */ }
// and NOT önmouseout or önmouseover



循环中的操作尚不清楚:您以相同的方式处理不同列表项元素的鼠标事件;以相同的方式修改同一元素element的属性.为什么?考虑一下.

—SA



What you are doing in the cycle is not clear: you handle mouse events of different list item elements in the same way; modify properties of the same element element in the same way. Why? Just think about it.

—SA


这篇关于mouseover事件中的引用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆