[英]Convert jquery to javascript to highlight parent div of radio button/checkbox
The following code adds a class to the parent of the checkbox / radio button to highlight the radio button along with the label.以下代码将 class 添加到复选框/单选按钮的父级,以突出显示单选按钮以及 label。
Here is the jQuery code which I need to convert to JavaScript. I am new to both jQuery and JavaScript.这是我需要将其转换为 JavaScript 的 jQuery 代码。我是 jQuery 和 JavaScript 的新手。
Any pointers will be appreciated !!任何指针将不胜感激!
$('form input[type=checkbox]').click ( function(){
var $this = $(this);
if (this.checked)
{ $(this).parent().addClass("highlight"); }
else
{ $(this).parent().removeClass("highlight"); }
})
$('form input[type=radio]').change(function(){
var $this = $(this);
$this.parent().parent().find('label.highlight').removeClass('highlight');
$(this).parent().addClass("highlight");
});
I think I found the solution to this.我想我找到了解决方案。
But I am sure this is not the most efficient code so if you can think of better options then do let me know.但我确信这不是最有效的代码,所以如果你能想到更好的选择,请告诉我。
function checkBox(obj) {
object=obj;
if (object.checked){
object.parentElement.classList.add("highLight1");
}
else {
object.parentElement.classList.remove("highLight1");
}
}
function highlight(obj) {
object = obj;
var radios = document.getElementsByName("A1");
for(var i = 0; i < radios.length; i++) {
if(radios[i].checked){
radios[i].parentElement.classList.add("highLight1");
}
else {
radios[i].parentElement.classList.remove("highLight1");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.