Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用jQuery替换数值ID的所有XX属性_Javascript_Jquery_Html - Fatal编程技术网

Javascript 用jQuery替换数值ID的所有XX属性

Javascript 用jQuery替换数值ID的所有XX属性,javascript,jquery,html,Javascript,Jquery,Html,我试图通过单击a元素来克隆div元素。当克隆该div时,所有XX属性都会更改为其数字ID。 例如: for=“gr\u name\u XX”tofor=“gr\u name\u 1” name=“grfield[XX][name]”至name=“grfield[1][name]” id=“gr\u name\u XX”至id=“gr\u name\u 1” 我正在尝试这样做,但只有div元素的ID属性执行此函数并更改为数字ID。对不起,我对javascript和jQuery编码还不熟悉。这是

我试图通过单击
a
元素来克隆
div
元素。当克隆该
div
时,所有XX属性都会更改为其数字ID。 例如:

  • for=“gr\u name\u XX”
    to
    for=“gr\u name\u 1”
  • name=“grfield[XX][name]”
    name=“grfield[1][name]”
  • id=“gr\u name\u XX”
    id=“gr\u name\u 1”
我正在尝试这样做,但只有
div
元素的ID属性执行此函数并更改为数字ID。对不起,我对javascript和jQuery编码还不熟悉。这是我的密码:

var-elementCounter=0;
jQuery(文档).ready(函数(){
jQuery(“添加新游戏”)。单击(函数(){
var elementRow=jQuery(“#占位符项”).clone();
var newId=“gr项目-”+元素计数器;
elementRow.attr(“id”,newId);
elementRow.show();
//我哪里有问题!!
elementRow.each(函数(){
elementRow.html().replace(/XX/g,elementCounter);
});
var removeLink=jQuery(“a”,elementRow)。单击(函数(){
removeElement(elementRow);
返回false;
});
elementCounter++;
jQuery(“输入[name=element max id]”).val(elementCounter);
jQuery(“.addandremovitems”).append(elementRow);
返回false;
});
});
函数移除元素(元素){
jQuery(element.remove();
}

姓名:
体裁:

背景: 日期: 字符串日期

您不需要使用
每个
,而且
.html()
只返回标记内容。要进行更新,您需要执行
.html(YOUR-UPDATE-code)

变,

// Where I have problem!!
elementRow.each(function() {
  elementRow.html().replace(/XX/g, elementCounter);
});

演示:

var-elementCounter=0;
jQuery(文档).ready(函数(){
jQuery(“添加新游戏”)。单击(函数(){
var elementRow=jQuery(“#占位符项”).clone();
var newId=“gr项目-”+元素计数器;
elementRow.attr(“id”,newId);
elementRow.show();
html(elementRow.html().replace(/XX/g,elementCounter));
var removeLink=jQuery(“a”,elementRow)。单击(函数(){
removeElement(elementRow);
返回false;
});
elementCounter++;
jQuery(“输入[name=element max id]”).val(elementCounter);
jQuery(“.addandremovitems”).append(elementRow);
返回false;
});
});
函数移除元素(元素){
jQuery(element.remove();
}

姓名:
体裁:

背景: 日期: 字符串日期

您不需要使用
每个
,而且
.html()
只返回标记内容。要进行更新,您需要执行
.html(YOUR-UPDATE-code)

变,

// Where I have problem!!
elementRow.each(function() {
  elementRow.html().replace(/XX/g, elementCounter);
});

演示:

var-elementCounter=0;
jQuery(文档).ready(函数(){
jQuery(“添加新游戏”)。单击(函数(){
var elementRow=jQuery(“#占位符项”).clone();
var newId=“gr项目-”+元素计数器;
elementRow.attr(“id”,newId);
elementRow.show();
html(elementRow.html().replace(/XX/g,elementCounter));
var removeLink=jQuery(“a”,elementRow)。单击(函数(){
removeElement(elementRow);
返回false;
});
elementCounter++;
jQuery(“输入[name=element max id]”).val(elementCounter);
jQuery(“.addandremovitems”).append(elementRow);
返回false;
});
});
函数移除元素(元素){
jQuery(element.remove();
}

姓名:
体裁:

背景: 日期: 字符串日期

首先获取并循环元素行的子元素,然后循环该子元素的每个属性:

  // get and loop through each children
  $(elementRow).children().each(function () {
     var child = $(this)[0];

     // loop through each attribute
     for (var i = 0; i < child.attributes.length; i++)
     {
        var attributeVal = child.attributes[i].value;

        // check if attribute value contains XX
        if (attributeVal.indexOf("XX") != -1)
        {
            // replace it
            child.attributes[i].value = attributeVal.replace(/XX/g, elementCounter)
        }
     }
  });
//获取并循环遍历每个子项
$(elementRow).children().each(函数(){
var child=$(this)[0];
//循环遍历每个属性
对于(var i=0;i
$(这)[0]
是一种获取
原始javascript HTML对象引用的方法,而不是获取
jQuery对象

.attributes
jshtml对象的属性
,在
jqueryhtml对象中不存在


希望有帮助。

您要做的是首先获取并循环您的
元素行的子元素,然后循环该子元素的每个属性:

  // get and loop through each children
  $(elementRow).children().each(function () {
     var child = $(this)[0];

     // loop through each attribute
     for (var i = 0; i < child.attributes.length; i++)
     {
        var attributeVal = child.attributes[i].value;

        // check if attribute value contains XX
        if (attributeVal.indexOf("XX") != -1)
        {
            // replace it
            child.attributes[i].value = attributeVal.replace(/XX/g, elementCounter)
        }
     }
  });
//获取并循环遍历每个子项
$(elementRow).children().each(函数(){
var child=$(this)[0];
//循环遍历每个属性
对于(var i=0;i
$(这)[0]
是一种获取
原始javascript HTML对象引用的方法,而不是获取
jQuery对象

.attributes
jshtml对象的属性
,在
jqueryhtml对象中不存在

希望有帮助