简体   繁体   English

如何使用jquery内部的click方法访问下一个父元素?

[英]How to access the next parent element using jquery inside click method?

How to access the next parent element using jquery inside click method? 如何使用jquery内部的click方法访问下一个父元素?

  $(document).ready(function () { $(".lv1 li").click(function (event) { this.parentElement.parentElement.textContent = this.textContent; $.ajax({ type: 'post', url: '@Url.Action("myAction", "Mycontroler")',// MVC method: generate url for backend; dataType: 'json', data: { InitiateId: $(this).val(), InitiateType: "Discipline" }, success: function (data) { $("blabla").removeClass('betimes'); // action with data aplied on element sub2 } }); }); }); 
 // i'm happy here .root-head ul{list-style:none;padding:1px 1px;overflow:hidden;}.root-head ul li{display:none;}.root-head ul li.level{display:flex;float:left;color:#777;padding-left:3px;}.root-head ul li.level:before{content:" > ";}.root-head ul li.level:hover{color:#999;}.root-head ul li.level.options.betimes{display:none;}.root-head ul li.level.options .option-selector{display:none;position:absolute;top:30px;padding:10px;border:2px solid #777;border-radius:10px;width:150px;}.root-head ul li.level.options .option-selector li{display:block;text-align:center;color:#777;}.root-head ul li.level.options:hover .option-selector{display:block;}.root-head ul li.level.options:hover .option-selector li{display:block;}.root-head ul li.level.options:hover .option-selector li:hover{border-bottom:2px solid #777;color:#999;transition:linear .5s ease-in 0s;}.root-head ul li.level:first-child{padding-left:20px;} 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <div class="root-head"> <ul> <li class="level">main</li> <li class="level options"> sub1 <ul class="option-selector lv1"> <li value="1">option1</li> <li value="2">option2</li> <li value="3">option2</li> <li value="4">option3</li> <li value="5">option4</li> <li value="6">option5</li> </ul> </li> <li class="level options betimes"> sub2 </li> <li class="level options betimes"> sub3 </li> </ul> </div> 

I wont to make some changes when I press click on target element dependency by the value of the option what I chose! 当我按我选择的选项的值单击目标元素依赖项时,我不会做任何更改!

How to access the next parent of the parent element? 如何访问父元素的下一个父元素?

Restriction: 限制:

I wont to be sure of it is in this block! 我不会确定它是否在这个街区中! Because of my Html structure contains this by multiple time! 由于我的HTML结构多次包含此内容!

  1. After $(".lv1 li").click(function (event) { add var a = this; $(".lv1 li").click(function (event) {添加var a = this;

  2. Replace $("blabla").removeClass('betimes'); 替换$("blabla").removeClass('betimes'); to $(a).closest('.options').next('.betimes').removeClass('betimes'); $(a).closest('.options').next('.betimes').removeClass('betimes');

Use event.target.parentElement.nodeName 使用event.target.parentElement.nodeName

List is parentElement attributes: https://www.w3schools.com/jsref/dom_obj_all.asp 列表是parentElement属性: https : //www.w3schools.com/jsref/dom_obj_all.asp

 $(document).ready(function () { $(".lv1 li").click(function (event) { //event.target.parentElement.nodeName = this.textContent; alert(event.target.parentElement.nodeName); }); }); 
 // i'm happy here .root-head ul{list-style:none;padding:1px 1px;overflow:hidden;}.root-head ul li{display:none;}.root-head ul li.level{display:flex;float:left;color:#777;padding-left:3px;}.root-head ul li.level:before{content:" > ";}.root-head ul li.level:hover{color:#999;}.root-head ul li.level.options.betimes{display:none;}.root-head ul li.level.options .option-selector{display:none;position:absolute;top:30px;padding:10px;border:2px solid #777;border-radius:10px;width:150px;}.root-head ul li.level.options .option-selector li{display:block;text-align:center;color:#777;}.root-head ul li.level.options:hover .option-selector{display:block;}.root-head ul li.level.options:hover .option-selector li{display:block;}.root-head ul li.level.options:hover .option-selector li:hover{border-bottom:2px solid #777;color:#999;transition:linear .5s ease-in 0s;}.root-head ul li.level:first-child{padding-left:20px;} 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <div class="root-head"> <ul> <li class="level">main</li> <li class="level options"> sub1 <ul class="option-selector lv1"> <li value="1">option1</li> <li value="2">option2</li> <li value="3">option2</li> <li value="4">option3</li> <li value="5">option4</li> <li value="6">option5</li> </ul> </li> <li class="level options betimes"> sub2 </li> <li class="level options betimes"> sub3 </li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM