Jquery regex从克隆元素中取出0并插入

jquery regex out 0 from cloned element and insert it?

本文关键字:插入 元素 regex Jquery      更新时间:2023-09-26

我想学习如何从克隆内部的所有元素中正则化出0,并将其替换为var count_rows。

//Duplicate table tr for multiple subtitle entry
$("#add_subtitle").on("click", function(){
    var count_rows = $(".subs_row").length;
    $("#sub_0").clone().insertBefore("#append_sub");
    return false;
});

标记:

<tr valign="top" id="sub_0" data-num="0" class="subs_row">
                    <th scope="row"><label for="subtitle">Subtitle_0</label></th>
                        <td>
                                <input name="subtitle[0][url]" type="text" value="<?php echo $wovies_extra_data[0]['subtitle'][0]['url']; ?>" style="width:280px;">
                                <p class="howto">Url</p>
                        </td>
                        <td>
                                <input name="subtitle[0][lang]" type="text" value="<?php echo $wovies_extra_data[0]['subtitle'][0]['lang']; ?>" style="width:100px;">
                                <p class="howto">Language: Label</p>
                        </td>
                        <td>
                                <input name="subtitle[0][lang_code]" type="text" value="<?php echo $wovies_extra_data[0]['subtitle'][0]['lang_code']; ?>" style="width:50px;">
                                <p class="howto">Language: Code</p>
                        </td>
                        <td>
                                <input name="subtitle_def" type="radio" value="<?php echo $key; ?>" <?php if ($wovies_extra_data[0]['subtitle_def'] == $key) {echo "checked";} ?> style="width:100px;">
                                <p class="howto">Default?</p>
                        </td>
                    </tr>

哦,我怎么能让我的regex不触零宽度:50px;div ?

在这里使用Regexp可能是错误的。
我建议使用一个模板系统,如http://mustache.github.io/或下划线

如果你真的想替换现有HTML代码中的零,你可以只针对某些属性:

function replaceZero(row) {
    return function (i, value) {
        return value.toString().replace(0, row);
    }
}
var $row = $("#sub_0");
$("label", $row).text(replaceZero(200));
$("input", $row).attr("name", replaceZero(200));
$row.attr("id", replaceZero(200));

小提琴(简化)Fiddle (with clone)