2011-11-30 55 views
0

在我的web应用程序的登录表单中,我使用jQuery交换密码字段的文本字段,一旦它获得焦点。它的工作无处不在,除了IE8(和,我假设,下面 - 尽管我没有检查)jQuery交换文本字段的密码字段不工作在IE8

var originalField = $(this);   // Original field 
var newField = $(this).clone();  // New field, cloned from the old one 
newField.attr("type", "password"); // Change the new field's type to password 
newField.insertBefore(originalField); // Insert the new field 
originalField.remove();    // Remove the old one 
newField.attr("id", "password");  // Give new field the id "password" (for CSS) 
newField.select();     // Bring focus to the new field 

的思考?

编辑:对不起,我没有指定,但IE中的“不工作”意味着该领域根本没有被取代。当我在IE中输入密码字段时,我可以阅读它的内容。

编辑2:下面是用HTML和JS的小提琴:http://jsfiddle.net/franktisellano/v5D9W/3/

+0

为什么它不是在IE8的工作?它不克隆该元素,它是否不插入克隆?它错误吗? –

+1

它究竟做什么而不是工作? newField.select()应该是newField.focus()吗? – Maxx

+0

你能设置一个包含HTML以及JS的小提琴吗? –

回答

0

IE8不允许你改变输入的类型。在开发工具Error: This command is not supported.<input class="custom" title=Password value="" type=password name=password placeholder="Password">中出现这样的错误。

jquery.placeholder插件通过创建新元素并从旧元素中复制属性来解决此问题。

function extractAttributes(elem) { 
    var attr = elem.attributes, copy = {}, skip = /^jQuery\d+/; 
    for(var i = 0; i < attr.length; i++) { 
     if(attr[i].specified && !skip.test(attr[i].name)) { 
      copy[attr[i].name] = attr[i].value; 
     } 
    } 
    return copy; 
} 

$clone = $('<input/>') 
    .attr($.extend(extractAttributes(this), {type: 'text', value: placeholder, 'data-password': 1, id: cid})) 
    .addClass('placeholder'); 

https://github.com/matoilic/jquery.placeholder/blob/master/jquery.placeholder.js#L80-L82