Home >Web Front-end >HTML Tutorial >$(this).html(); 输出的内容有点疑惑,求解~谢过_html/css_WEB-ITnose

$(this).html(); 输出的内容有点疑惑,求解~谢过_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-21 09:13:111432browse

<html><head></head> <script src="__PUBLIC__/Js/jquery-1.8.3.min.js"></script>  <script> 	$(document).ready(function(){ 		$("#menu_id ul ul").css("display","block"); 		$("#menu_id ul li").click(function(){ 			console.log($(this).html()); 		}); 	}); </script><body>	<div style="position:relative; overflow:auto;" id="menu_id">		<ul>			<!-- 第一个:Li -->			<li>				<span>gaga</span>				<ul>					<li>						ABCD						<ul>							<li><a href="#">a</a></li>							<li><a href="#">a</a></li>							<li><a href="#">a</a></li>							<li><a href="#">a</a></li>							<li><a href="#">a</a></li>						</ul>					</li>					<li>						<span>ABCD</span>						<ul>							<li><a href="#">b</a></li>							<li><a href="#">b</a></li>							<li><a href="#">b</a></li>							<li><a href="#">b</a></li>							<li><a href="#">b</a></li>						</ul>					</li>					<li>						<span>ABCD</span>						<ul>							<li><a href="#">c</a></li>							<li><a href="#">c</a></li>							<li><a href="#">c</a></li>							<li><a href="#">c</a></li>							<li><a href="#">c</a></li>						</ul>					</li>					<li>						<span>ABCD</span>						<ul>							<li><a href="#">d</a></li>							<li><a href="#">d</a></li>							<li><a href="#">d</a></li>							<li><a href="#">d</a></li>							<li><a href="#">d</a></li>						</ul>					</li>				</ul>			</li>		</ul>	</div></body></html>


我点击Li后输出的不止是当前的元素下的Html()输出了,还有根节点的Li的Html()也输出了,为什么啊?给个合理的解释,采纳给分哦,么么哒


回复讨论(解决方案)

因为事件会向上冒泡的
.

 	$(document).ready(function(){ 		$("#menu_id ul ul").css("display","block"); 		$("#menu_id ul li").click(function(event){ 			console.log($(this).html());			event.stopPropagation(); 		}); 	});

$("#menu_id ul") 下面有两个li嘛,首先打印里面li的内容  然后是外面的li的内容。 要是想打印最里层的那你不如再加个id

谢两位,问题解决,么么哒

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn