Foreach标签获取id - 修改此id - 添加到父类 [英] Foreach label get id - modify this id - add to parent class
问题描述
control-group
Joomla模块的基本构建:
< div class =control-group>
< div class =control-label>
< / div>
< div class =controls>< / div>
< / div>
通用的最佳方法是什么
(function($){
$(文件).ready(function(){
$(#jform_params_some_text_here-lbl)。parents(.control-group).addClass(jform_params_some_text_here-cg);
});
})(jQuery);
但是我需要拥有它。
< h1>我想做什么
- Foreach标签获取标识符。
- 删除字母
lbl
并换成字母cg
- 用类
control-group
- 将刚刚重命名的类添加到此父级
- 删除字母
感谢大家的帮助!
您可以利用jQuery的具有属性选择器,只能定位具有 然后,您应该能够看到您在下面注释的首选步骤: I am trying to make a universal code that can be used in every module for the Joomla CMS Backend. The code needs to grab the ID from the labels and add this id as a class to the nearest parent with the class id $ c的标签元素
//步骤1:迭代每个具有`id`属性的标签
$('label [id]')。each(function(){
//步骤2:获取替换ID
var id = $(this).attr('id')。replace('lbl','cg');
//第三步:用control-group类查找离您最近的父类
var parent = $(this).parents('。control-group');
//第四步:现在为你的父元素设置类
parent.addClass(id);
});
control-group
Basic build up of a Joomla Module:
<div class="control-group">
<div class="control-label">
<label id="jform_params_some_text_here-lbl" for="jform_params_some_text_here" class="hasPopover"></label>
</div>
<div class="controls"></div>
</div>
What is the best way to do this universally
I know that I can set it individual with this:
(function ($) {
$(document).ready(function() {
$("#jform_params_some_text_here-lbl").parents(".control-group").addClass("jform_params_some_text_here-cg");
});
})(jQuery);
But I need to have it universal.
What do I want to achieve
- Foreach label get the id.
- Remove the letters
lbl
and replace with the letterscg
- Find nearest parent with class
control-group
- Add the just renamed class to this parent
Thanks to everyone for helping!
You can take advantage of jQuery's available has attribute selector to only target label elements with a defined id
attribute to avoid any undefined errors.
You should then be able to see your preferred steps annotated below :
// Step 1: Iterate through each label that has an `id` attribute
$('label[id]').each(function(){
// Step 2: Get your replacement id
var id = $(this).attr('id').replace('lbl','cg');
// Step 3: Find your nearest parent with "control-group" class
var parent = $(this).parents('.control-group');
// Step 4: Now set the class for your parent element
parent.addClass(id);
});
这篇关于Foreach标签获取id - 修改此id - 添加到父类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!