[英]How can I select ID and get a value of it in input data in this row?
here is what I want to do. 这是我想做的。
When I click the modify button, I wish div that contains role of 'ucomment' changes into in-line input form with its original value typed. 当我单击“修改”按钮时,希望包含“ ucomment”角色的div更改为内联输入形式,并键入其原始值。
But I can't even start with selecting the div. 但是我什至不能从选择div开始。
I thought the code here would work and could select what I aimed, but it didn't. 我认为这里的代码可以工作,并且可以选择我想要的目标,但是没有。
$(this).closest("div[role='ucomment']")
How can I select the div here? 如何在这里选择div?
<ul id="comments" class="list-group">
<script id="commentTemplate" type="text/x-handlebars-template">
{{#each list}}
<li class="list-group-item d-flex justify-content-between align-items-center">
<div class="row w-100">
<div class="col-2" name="uid">{{uid}}</div>
<div class="col-8" role="ucomment" name="ucomment">{{ucomment}}</div>
<div class="col-2" name="regdate">{{prettifyDate regdate}}</div>
</div>
<button type="button" class="btn" onclick="modifyComment()">Modify</button>
<button type="button" class="btn" onclick="deleteComment()">Delete</button>
<span class="badge badge-primary badge-pill">14</span>
</li>
{{/each }}
</script>
</ul>
<button id="sendForm" type="submit" class="btn btn-info" onclick="addComment()">Send</button>
<button id="cancel" type="button" class="btn btn-warning" data-toggle="collapse" data-target="#openNew">Cancel</button>
</div>
This should find you the div
: 这应该找到div
:
this.closest("li").querySelector("div[role='ucomment']")
closest()
works only upwards in the DOM. closest()
仅在DOM中向上工作。 It traverses the parent elements. 它遍历父元素。 So what you can find using it is li
, from there you can use find()
using the selector div[role='ucomment']
you tried to pass to closest()
. 因此,您可以找到使用li
,从那里可以使用尝试传递给closest()
的选择器div[role='ucomment']
使用find()
closest()
。
// get a NodeList containing alls modify buttons and convert it to an array const modifyBtns = Array.from(document.querySelectorAll('.btn.btn-modify')); // iterate over each button... for (const modifyBtn of modifyBtns) { // and attach an event listener for click, binding the handler to each modifyBtn modifyBtn.addEventListener('click', modifyComment.bind(modifyBtn)); } function modifyComment(e) { this.closest("li").querySelector("div[role='ucomment']").style.color= 'red'; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="list-group-item d-flex justify-content-between align-items-center"> <div class="row w-100"> <div class="col-8" role="ucomment" name="ucomment1">{{ucomment}}</div> </div> <button type="button" class="btn btn-modify">Modify</button> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <div class="row w-100"> <div class="col-8" role="ucomment" name="ucomment2">{{ucomment}}</div> </div> <button type="button" class="btn btn-modify">Modify</button> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <div class="row w-100"> <div class="col-8" role="ucomment" name="ucomment1">{{ucomment}}</div> </div> <button type="button" class="btn btn-modify">Modify</button> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <div class="row w-100"> <div class="col-8" role="ucomment" name="ucomment2">{{ucomment}}</div> </div> <button type="button" class="btn btn-modify">Modify</button> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <div class="row w-100"> <div class="col-8" role="ucomment" name="ucomment1">{{ucomment}}</div> </div> <button type="button" class="btn btn-modify">Modify</button> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <div class="row w-100"> <div class="col-8" role="ucomment" name="ucomment2">{{ucomment}}</div> </div> <button type="button" class="btn btn-modify">Modify</button> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <div class="row w-100"> <div class="col-8" role="ucomment" name="ucomment1">{{ucomment}}</div> </div> <button type="button" class="btn btn-modify">Modify</button> </li> <li class="list-group-item d-flex justify-content-between align-items-center"> <div class="row w-100"> <div class="col-8" role="ucomment" name="ucomment2">{{ucomment}}</div> </div> <button type="button" class="btn btn-modify">Modify</button> </li> </ul>
Edit: By the looks of it using the in line eventhandler onclick
won't pass the event, so I changed your code to vanilla JS instead of jQuery (apparently closest in jQuery doesn't work the way plain JS closest works). 编辑:通过使用行内事件处理程序onclick
的外观,它不会传递事件,因此我将代码更改为香草JS而不是jQuery(显然,jQuery中最接近的方式无法像普通JS最接近的方式那样工作)。 Since using inline event handlers is considered bad practice anyway (that's what addEventListener()
is for), you should change that anyway. 由于无论如何都使用内联事件处理程序被认为是不好的做法(这就是addEventListener()
目的),因此您仍然应该更改它。
Can't you use plain javascript for this? 您不能为此使用普通的javascript吗?
document.getElementsByName("ucomment")[0];
Let me know if this works. 让我知道这个是否奏效。
Then you can use jQuery's click function to get the clicking element and check if the clicked element is a "div" and do whatever the things you want to do. 然后,您可以使用jQuery的click函数获取click元素,并检查clicked元素是否为“ div”,然后执行您想做的任何事情。
$("*", document.body).click(function(e) {
e.stopPropagation();
var domElement = $(this).get(0);
if (domElement.tagName == "div") {
//do what you want to do here
}
});
When you click on button that call modifyComment() you need up to parent and find your div. 当您单击调用modifyComment()的按钮时,需要父级并找到您的div。 try to change html onclick and function 尝试更改html onclick和功能
<button type="button" class="btn" onclick="modifyComment(this)">Modify</button>
function modifyComment(elem) {
let parent = $(elem).parent();
parent.find($("div[role='ucomment']")).css("background", "red");
console.log(parent);
}
Or if you change html button then inside function modifyComment 或者,如果您更改html按钮,则在函数ModifyComment内
$(element).parent().find("div[role='ucomment']").css("background-color","red")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.