Javascript IE7中的HTMLInputElement

Javascript IE7中的HTMLInputElement,javascript,dom,internet-explorer-7,Javascript,Dom,Internet Explorer 7,我正在为交叉浏览器输入和文本区域选择getter和setter编写一个扩展。 这就是我编写代码的方式: HTMLInputElement.prototype.getSelectionRange = get_selection_range; HTMLInputElement.prototype.setSelection = set_selection_range; HTMLTextAreaElement.prototype.getSelectionRange = get_selection_ran

我正在为交叉浏览器输入和文本区域选择getter和setter编写一个扩展。 这就是我编写代码的方式:

HTMLInputElement.prototype.getSelectionRange = get_selection_range;
HTMLInputElement.prototype.setSelection = set_selection_range;
HTMLTextAreaElement.prototype.getSelectionRange = get_selection_range;
HTMLTextAreaElement.prototype.setSelection = set_selection_range;
get_selection_range和set_selection_range是这些扩展函数。所以我只想替换

someInputElement.selectionStart = a;  // and whole lot of code due to browser
someInputElement.selectionEnd = b;    // compatibility
仅仅

someInputElement.setSelection(a, b);
someInputElement.setSelection({ start: a, end: b });
someOtherElement.setSelection(someInputElement.getSelection());
但后来我在IE7中遇到了一些困难。 首先,IE7不知道什么是HTMLInputElement

我不想扩展整个对象。那是我最不愿意做的事,但我想逃避。 函数get_selection_range和set_selection_range都可以,不要问里面有什么,你已经看过好几次了

所以问题是:JS中的HTMLInputElement是否有合法的替代IE7

UPD:我已经制定了自己的解决方案,没有扩展任何全局对象类型:

var SmartInputSelection = Base.extend({
    constructor: function (options) {
        this.node = options.node;
        this.CHARACTER = "character";
        this.END_TO_END = "EndToEnd";
        this.END_TO_START = "EndToStart";
    },
    setSelection: function (a, b) {
        if (b === undefined && typeof a == "number")
            b = a;
        else if (b === undefined) {
            b = a.end;
            a = a.start;
        }

        if (this.node.selectionStart !== undefined) {
            this.node.selectionStart = a;
            this.node.selectionEnd = b;
        } else {
            var textRange = this.node.createTextRange();
            textRange.collapse(true);
            textRange.moveStart(this.CHARACTER, a);
            textRange.moveEnd(this.CHARACTER, b - a);
            textRange.select();
        }
    },
    getSelection: function () {
        var start, end;
        if (this.node.selectionStart !== undefined) {
            start = this.node.selectionStart;
            end = this.node.selectionEnd;
        } else {
            var range = document.selection.createRange();
            if (range == null) {
                start = end = 0;
            }

            var textRange = this.node.createTextRange(),
            duplicate = textRange.duplicate();
            textRange.moveToBookmark(range.getBookmark());
            duplicate.setEndPoint(this.END_TO_END, textRange);
            end = duplicate.text.length;
            duplicate.setEndPoint(this.END_TO_START, textRange);
            start = duplicate.text.length;
        }

        return {
            start: start,
            end: end
        };
    }
});
现在我只需要声明如下内容:

function SelectSomeTextInsideInput (input /* input is some DOM element */) {
    var smartInputSelection = new SmartInputSelection({ node: input });
    smartInputSelection.setSelection(0);
    smartInputSelection.setSelection(2, 5);
    smartInputSelection.setSelection({ start: 2, end: 5 });
    smartInputSelection.getSelection(); // start: 2, end: 5
}
没有。 我最近在与IE9抗争,在那里我通过切换到HTML5模式实现了它。
我猜IE7已经太老了,无法以任何方式支持它

无论如何,请尝试将此语句放在页面顶部:

<!doctype HTML>

编辑:并在此处查看此答案:

否。 我最近在与IE9抗争,在那里我通过切换到HTML5模式实现了它。
我猜IE7已经太老了,无法以任何方式支持它

无论如何,请尝试将此语句放在页面顶部:

<!doctype HTML>


编辑:并在此处查看此答案:

您不应该依赖这些对象进行全局定义,因为您已经注意到在某些浏览器中它们不是。

您不应该依赖这些对象进行全局定义,因为您已经注意到在某些浏览器中它们不是。

Eval?真正地无论如何,这根本不是答案。我在最后一行做了一个tldr版本。评估?真正地无论如何,这根本不是答案。我在最后一行做了一个tldr版本。是的,这是某种回答。虽然,如果我是CMI,我不会给你赏金=)是的,这是某种回答。虽然,如果我是CMI,我不会给你赏金=)