Foreach标签获取id - 修改此id - 添加到父类 [英] Foreach label get id - modify this id - add to parent class

查看:225
本文介绍了Foreach标签获取id - 修改此id - 添加到父类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个通用代码,可以在Joomla CMS后端的每个模块中使用。代码需要从标签中获取ID,并将该ID作为一个类添加到最近的父类中,类为 control-group



Joomla模块的基本构建:



 < div class =control-group> 
< div class =control-label>



通用的最佳方法是什么



 (function($){
$(文件).ready(function(){
$(#jform_params_some_text_here-lbl)。parents(.control-group).addClass(jform_params_some_text_here-cg);
});
})(jQuery);

但是我需要拥有它。



< h1>我想做什么


  1. Foreach标签获取标识符。
  2. 删除字母 lbl 并换成字母 cg

  3. 用类 control-group

  4. 将刚刚重命名的类添加到此父级



感谢大家的帮助!


解决方案

您可以利用jQuery的具有属性选择器,只能定位具有 id

然后,您应该能够看到您在下面注释的首选步骤:

  //步骤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);
});


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 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

  1. Foreach label get the id.
  2. Remove the letters lbl and replace with the letters cg
  3. Find nearest parent with class control-group
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆