html - 为li添加边框时出现错误

标签 html css

	var ul=document.getElementsByTagName('ul')[0];
	ul.addEventListener('mouseover',function(e){
		if(e.target.tagName.toUpperCase()=="LI"){
			e.target.style.border='1px solid red';
		}
	})
	ul,li{
		margin:0px;
		padding:0px;
	}
	ul{
		margin:200px;
		width:560px;
		height:500px;
		border:1px solid red;
	}
	li{
		width:100px;
		height:100px;
		background:green;
		float:left;
		list-style:none;
		margin:0px 10px 10px 0px ;
	
	}
<html>
  <head>
    <meta name="generator"
    content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" />
    <title></title>

  </head>
  <body>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
    </ul>

  </body>
</html>

我写的代码,当鼠标从右到左第二行的li上,然后鼠标悬停在第一行,第二个li下降,如果给所有li标签加边框,错误就会消失,但我想知道为什么,谁能教我?谢谢。

最佳答案

var ul = document.getElementsByTagName('ul')[0];
ul.addEventListener('mouseover',function(e){
    if (e.target.tagName.toUpperCase() === "LI") {
        e.target.style.border = '1px solid red';
    }
})
ul, li {
    margin: 0px;
    padding: 0px;
}

ul {
    margin: 200px;
    width: 560px;
    height: 500px;
    border: 1px solid red;
}

li {
    width: 100px;
    height: 100px;
    background: green;
    float: left;
    list-style: none;
    margin: 0px 10px 10px 0px;
    border: 1px solid green;
}
<html>
  <head>
    <meta name="generator"
    content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" />
    <title></title>

  </head>
  <body>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
    </ul>

  </body>
</html>

关于html - 为li添加边框时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31158731/

相关文章:

javascript - 如何在 iPhone 上聚焦 HTML 文本字段(导致键盘出现)?

未应用 Jquery Datatables 插件 CSS 默认值。分页和搜索出现在表格的左侧。没有风格

css - 如何禁用 :hover affects rails twitter bootstrap

javascript - 解码后的 URI 在 HTML 中无法正确显示

html - 我的图像是否正在移动设备上加载?

html - 有没有理由不将主 css 文件嵌入到网页中?

css - Internet Explorer 11 向元素等添加随机样式

html - 仅使用 css 使 <div> 继承父级 (<td>) 的高度?

jQuery - 检查是否选择了任何选择选项或未选择所有选择选项

javascript - 在 var 函数中覆盖 jQuery 添加的内联样式