简体   繁体   English

替换克隆元素上所有属性的更好方法?

[英]Better way to replace all the attributes on cloned elements?

This code works, but it just seems a bit long in the tooth to me. 这段代码有效,但是对我来说似乎有点长。 I only tend to write JS/Jquery when I absolutely have to, so I'm quite the noob. 我只在绝对需要的时候才写JS / Jquery,所以我很菜鸟。

I'm cloning an element, then replacing all the attributes with one I've created. 我正在克隆一个元素,然后用我创建的元素替换所有属性。

Here's the JSFiddle, or code snippets found below: http://jsfiddle.net/og6Lcof0/3/ 这是下面的JSFiddle或代码段: http : //jsfiddle.net/og6Lcof0/3/

HTML: HTML:

<div id="eqblock" style="display:inline-block">

  <li id="equipment-0"><label for="equipment-0">Equipment</label> <select id="equipment-0" name="equipment-0"><option selected value="1">T700</option><option value="3">iLink</option></select></li>

  <li id="equipment-1"><label for="equipment-1">Equipment</label> <select id="equipment-1" name="equipment-1"><option selected value="1">T700</option><option value="3">iLink</option></select></li>

  <li id="equipment-2"><label for="equipment-2">Equipment</label> <select id="equipment-2" name="equipment-2"><option value="1">T700</option><option selected value="3">iLink</option></select></li>

</div>
<div id="modblock" style="display:inline-block">

  <li id="mod-0"><label for="mod-0">Method of Delivery</label> <input id="mod-0" name="mod-0" type="text" value="mod1">  <a href="#" id="remove_eq_0">Remove</a></li>

  <li id="mod-1"><label for="mod-1">Method of Delivery</label> <input id="mod-1" name="mod-1" type="text" value="mod2">  <a href="#" id="remove_eq_1">Remove</a></li>

  <li id="mod-2"><label for="mod-2">Method of Delivery</label> <input id="mod-2" name="mod-2" type="text" value="mod3">  <a href="#" id="remove_eq_2">Remove</a></li>

</div>

<a href="#" id="add_eq">Add Equipment</a>

JS/JQ: JS / JQ:

$( "#add_eq" ).click(function () {
    var num = findNum();
    if (typeof num !== 'undefined') {
        var $eqLabel = 'equipment-' + num;

        var $newEq = $( '#eqblock li:first' ).clone( false );
        $( $newEq ).attr( 'id', $eqLabel );
        $( 'label', $newEq ).attr( 'for', $eqLabel );
        $( 'select', $newEq ).attr( 'id', $eqLabel );
        $( 'select', $newEq ).attr( 'name', $eqLabel );
        $( "#eqblock" ).append( $newEq );

        var $modLabel = 'mod-' + num;
        var $newMod = $( '#modblock li:first' ).clone( false );
        $( $newMod ).attr( 'id', $modLabel );
        $( 'label', $newMod ).attr( 'for', $modLabel );
        $( 'input', $newMod ).attr( 'id', $modLabel );
        $( 'input', $newMod ).attr( 'name', $modLabel );
        $( 'input', $newMod ).attr( 'value', " ");
        $( 'input', $newMod ).val( '' );
        $( 'a', $newMod ).attr( 'id', 'remove_eq_' + num);
        $( "#modblock" ).append( $newMod );
    };
});

Here's an updated JSFiddle with my completed code for reference http://jsfiddle.net/og6Lcof0/4/ 这是更新的JSFiddle,其中包含我完整的代码,以供参考 http://jsfiddle.net/og6Lcof0/4/

Thanks everyone for the help! 谢谢大家的帮助!

The nature of your problem doesn't lend itself to full automation, but there are some syntax shortcuts you can use to reduce repetition. 问题的性质并不适合于完全自动化,但是可以使用一些语法快捷方式来减少重复。 Instead of: 代替:

    $( 'label', $newMod ).attr( 'for', $modLabel );
    $( 'input', $newMod ).attr( 'id', $modLabel );
    $( 'input', $newMod ).attr( 'name', $modLabel );
    $( 'input', $newMod ).attr( 'value', " ");
    $( 'input', $newMod ).val( '' );

Try: 尝试:

    $( 'label', $newMod ).attr( 'for', $modLabel );
    $( 'input', $newMod )
        .attr({
            id: $modLabel,
            name: $modLabel,
            value: " "
        })
       .val( '' );

You can greatly simplify your HTML, which will probably enable you to simplify your JavaScript. 您可以极大地简化HTML,这可能使您可以简化JavaScript。

Currently, you have duplicate IDs, which is not allowed in HTML: 当前,您有重复的ID,HTML中不允许使用这些ID:

<li id="equipment-0">
  <label for="equipment-0">Equipment</label>
  <select id="equipment-0" name="equipment-0">
    <option selected value="1">T700</option>
    <option value="3">iLink</option>
  </select>
</li>

You probably don't need an ID for li , so remove it. 您可能不需要li的ID,因此将其删除。

You can also remove the labels' for by making the select and input elements a child of the labels. 您还可以删除标签for通过使selectinput元素的标签的孩子。 After you've done that, you may no longer need an id on the select and input elements. 完成此操作后,可能不再需要selectinput元素上的id

That reduces the above HTML to this: 这将上述HTML简化为:

<li>
  <label>Equipment
    <select name="equipment-0">
      <option selected value="1">T700</option>
      <option value="3">iLink</option>
    </select>
  </label>
</li>

At this point, you're left with only one attribute that needs changing after cloning. 此时,克隆后只剩下一个需要更改的属性。

Short answer: There isn't really a better way. 简短答案:确实没有更好的方法。

Long answer: 长答案:

You could assign your jQuery objects into a variable so you don't have to repeat the query selector as much. 您可以将jQuery对象分配给一个变量,因此您不必重复太多的查询选择器。

eg 例如

$( 'input', $newMod ).attr( 'id', $modLabel );
$( 'input', $newMod ).attr( 'name', $modLabel );
$( 'input', $newMod ).attr( 'value', " ");
$( 'input', $newMod ).val( '' );

becomes 变成

var newModInput = $( 'input', $newMod );
newModInput.attr( 'id', $modLabel );
newModInput.attr( 'name', $modLabel );
newModInput.attr( 'value', " ");
newModInput.val( '' );

This also could be slightly more efficient due to not having to traverse the $newMod to find the input every time. 由于不必每次都遍历$ newMod来查找输入,因此这也可能稍微更有效。 Not really enough to notice though. 虽然还不足以引起注意。

you can simplify templating using handlebars , you can take a look at here( http://handlebarsjs.com/ ) Handlebars works well with JQuery. 您可以使用handlebars简化模板,您可以在此处看看( http://handlebarsjs.com/)Handlebars与JQuery配合良好。

you can create template (attach this script tags in html) 您可以创建模板(在html中附加此脚本标签)

<script id="equipment-template" type="text/x-handlebars-template">
<li id="{{eqLabel}}">
    <label for="{{eqLabel}}">Equipment</label>
    <select id="{{eqLabel}}" name="{{eqLabel}}">
        <option selected value="1">T700</option>
        <option value="3">iLink</option>
    </select>
</li>
</script>

if you see the above template contains the key "eqLabel", these are the dynamic variables replaced by json. 如果您看到上面的模板包含键“ eqLabel”,则这些是被json替换的动态变量。

in your domready 在你的家中

var equipmentTemplate   = $("#equipment-template").html();
var $equipmentTemplate = Handlebars.compile(equipmentTemplate);

$equipmentTemplate contains the compiled equipmentTemplate, pass the json with proper key:value pairs to $equipmentTemplate it will replace the keys with values $ equipmentTemplate包含已编译的equipmentTemplate,将带有正确key:value对的json传递给$ equipmentTemplate,它将使用值替换密钥

var $eqLabel = 'equipment-' + num;
var eqContext = {eqLabel: $eqLabel};
var eqhtml    = $equipmentTemplate(eqContext);
$( "#eqblock" ).append( eqhtml );

you can do the same with modblock. 您可以使用modblock进行相同的操作。

example : http://jsfiddle.net/r3n5xjpv/ 示例: http//jsfiddle.net/r3n5xjpv/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM