0

我正在尝试更改字段属性,因为我使用了许多与 Firefox 和 Internet Explorer 兼容的输入类型日期,

我还想将输入类型日期更改为输入类型文本并添加 id 以添加 jquery datepicker。

我也完成了该代码:

   <script type="text/javascript" language="javascript">
    function changefield(){
        document.getElementsByTagNAme("date").innerHTML = "<input id=\"datepicker\" type=\"text\"  tabindex=\"2\" />";

    }
    chengefield()
</script>

而且它似乎不起作用。

我在控制台中没有 me'ssage 错误。

接受我最大的尊重。

亲切的问候。

SP

4

2 回答 2

1
function changefield(){
    // getElementsByTagName return a collection of elements (like an array)
    var inputs = document.getElementsByTagName("input");

    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'date') {

            // change the input's type to text
            inputs[i].setAttribute('type', 'text');

            // id for each element must be unique
            inputs[i].setAttribute('id', 'datepicker' + i);

            inputs[i].setAttribute('tabindex', i+2);
        }
    }

}
于 2012-09-18T07:30:17.133 回答
1

我想你正在尝试的是选择输入类型日期,而不是元素。而你谈到添加 jquery datepicker,那么为什么不使用 jQuery 呢?

$("input[type=date]").replaceWith("<input type='text' class='mydatepicker'/>");

接着

$(".mydatepicker").datepicker();

但不要将“id”与可以匹配多个元素的函数一起使用,因为这会生成重复的 id;也使用类,如果您想知道为什么不简单地更改输入类型,IE 不会允许这样做

于 2012-09-18T07:31:37.983 回答