开发者

Multi replace element name with mootools

is there any equivalent in moo that can replace multiple element id names

i have this

    $$('myelement').each(function(el){ 
            var get_all_labels = el.getElements('label');/
            var get_label_id = get_all_labels.getProperty('id');
            el.addClass(get_label_id);

        });

but mu labels (labael_name) return additional suffix like -elem,, and I need to remove -elem,, from the new created parent class name . I tested replace but probably in the wrong spot , it returns replace is not a function , also tested custom string_replace for js bu开发者_如何学Pythont im not having any luck with it , please any hint. Thnx!


this is somewhat wrong.

var get_all_labels = el.getElements('label'); - returns a collection

var get_label_id = get_all_labels.getProperty('id'); - returns an array of ids.

so if you have a single label, it will go:

[labelObject#someid-elem] which will then return ["someid-elem"]

the problem is, element.addClass requires a single string, not an array of strings but you can use array.join to work around that.

if you do have more than 1 label and need to add all of them as classes to the el you are looping, you can do

$$('myelement').each(function(el) {
    var get_all_labels = el.getElements('label');

    // get all ids and replace them... use .get for 1.2+ or .getProperty for 1.11
    var get_label_ids = get_all_labels.map(function(label) {
        return label.getProperty("id").replace("-elem", "");
    });

    // add to parent element.
    el.addClass(get_label_ids.join(" "));
    console.log(el);
});

if this is not the intended behavior and you actually have a single label, then just do

$$('myelement').each(function(el) {
    var id = el.getElement('label').get("id").replace("-elem", "")
    el.addClass(id);
    console.log(el);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜