0

In my page I have labels that look like this:

ContentPlaceHolder1_gvGroups_lblName_0

with corrosponding:

ContentPlaceHolder1_gvGroups_lblHidden_0

I can assert that any *lblName has a corrosponding *lblHidden.

Now, I have some js:

//use to make any label with 'edit' class editable
function makeLabelsEditable() {

    $(".edit").focusout(function () {
        setLabel(this);
    });

    $(".edit").click(function () {
        editLabel(this);
    });
}

//used to edit labels
function editLabel(source) {
    source.innerHTML = '<input type="text" maxlength="40" value="' + source.innerHTML + '"/>';
    $(source).unbind('click');
    source.children[0].focus()
}

//used to edit labels
function setLabel(source) {

    if (source.children[0].value != '') {
        $(source).click(function () {
            editLabel(this);
        });
        source.innerHTML = source.children[0].value;
    }

}

I can assert that anything marked with class edit will be valid.

I need to modify this code like so: //used to edit labels

function setLabel(source) {

    if (source.children[0].value != '') {
        $(source).click(function () {
            editLabel(this);
        });
        source.innerHTML = source.children[0].value;
        var hidden = someHowGetHiddenFromSource(source);
        hidden.innerHTML = source.children[0].value;
    }

}

I'm sure this is possible, I just do not know how.

Essentially, it would be something like:

getElementByID(replace(source.id,'lblName','lblHidden'));

I just do not know what JS / JQ functions can do that.

Thanks

4

1 回答 1

0

本质上,它会是这样的: getElementByID(replace(source.id,'lblName','lblHidden'));

几乎就是这样。它的:

var hidden = document.getElementById(source.id.replace('lblName', 'lblHidden'));

hidden将是 DOM 元素。

或者使用 jQuery:

var hidden = $("#" + source.id.replace('lblName', 'lblHidden'));

hidden将是一个包装元素的 jQuery 实例。

DOM 元素的id属性是一个字符串。JavaScript 字符串有一个replace函数,它在简单的情况下接受要替换的字符串和替换字符串。(除此之外还有更多内容,但这就是您在这种情况下所需要的。)

于 2013-02-06T16:36:25.830 回答