
P元素是不能包含块级元素(包括P自身)
你可以试一下下面的代码,11并没有变色。
而且实际运用的时候一般都会加上id或者class的。不会这样一次获取全体元素的。
对于这种嵌套的研究,最好用div实验。
<!doctype html><html>
<head>
<title>demo</title>
<meta charset="utf-8"/>
<script type="text/javascript" src="js/jquery-311minjs"></script>
<style type="text/css">
</style>
<script>
$(function(){
$("p")siblings()css("color", "red")
})
</script>
</head>
<body>
<p>11</p>
<span>22<p>33</p></span>
</body>
</html>
可以使用Jquery中的siblings()和remove()方法实现。实现原理主要是使用siblings获取被点击元素之外的同级元素,然后使用remove()删除。完整的代码如下:
运行的效果如下:
例如当点击DIV内容为2的框时,其他的DIV被清除,内容为2的框移到最顶,点击后的效果图如下:
扩展资料:
如果想对移到最顶的框进行一定的突出显示处理,比如给个背景颜色,字体加大,可以利用css()方法,代码如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery例子</title>
<link href="__CSS__/basecss" rel="stylesheet">
<script src="__JS__/jqueryminjs"></script>
</head>
<body>
<script>
$(function(){
$("class1")click(function() {
$(this)siblings()remove();
$(this)css({"background-color":"yellow","font-size":"50px"});
});
});
</script>
<div class="main">
<div class="class1">1</div>
<div class="class1">2</div>
<div class="class1">3</div>
<div class="class1">4</div>
</div>
<style type="text/css">
main{width:640px;height:500px;margin:0 auto;border:1px solid red;}
class1{width:90%;height:100px;margin:0 auto;border:1px solid red;margin-top:10px;font-size:40px;}
</style>
</body>
</html>
运行的效果如下:
你可以让span相对于div来定位啊,这样就不用管input的长度和位置了。当然,input也要通过设置text-indent来给span腾出位置。下面是个简单的实现例子(为了方便,我用了内联的css,你可以改成外联的):
<div style="position:relative"><input type="text" id="xj_zhanghao" class="xj_text" maxlength="16" style="text-indent:20px" placeholder="账号" onKeyUp="xjzhltishi()" onFocus="qcxjtishi()"/><span id="xjzhtishi" style="position:absolute; left:3px; top:3px; width:14px; padding:1px; line-height:1; text-align:center; background-color:red; color:white; font-size:11px; border-radius:2px">99</span></div>
以上就是关于jquery里面的siblings()方法是当前元素寻找同胞元素,但有个地方不太明白全部的内容,包括:jquery里面的siblings()方法是当前元素寻找同胞元素,但有个地方不太明白、JQuery,如何清除,除了点击的元素以外,其他所有元素、css怎么实现相对同级元素定位等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)