更改<输入>使用具有跨浏览器支持的 jQuery 进行输入输入>
问题
以下脚本完全符合我的要求,在除 IE8 之外的所有测试浏览器中,它删除了按钮而不用任何东西替换它。
背景
我一直在研究一种使用 jQuery 将提交类型输入替换为图像类型输入的方法。
首先,我有以下适用于 FF 和 webkit 的脚本:
marker = jQuery('<span class="marker"></span>').insertBefore('input#SearchButton');
jQuery('input#SearchButton').detach().attr('type', 'image').attr('src',theme_folder+'/style/images/search_button.png').insertAfter(marker);
marker.remove();
为 IE 浏览器开发了以下脚本:
marker = jQuery('<span class="marker"></span>').insertBefore('input#SearchButton');
new_search = jQuery('<input id="SearchButton" value="Search" name="Searchbutton" type="image" src="'+theme_folder+'/style/images/search_button.png'+'" />')
jQuery(new_search).insertAfter('input#SearchButton');
jQuery('input#SearchButton').remove();
marker.remove();
但是如果 IE (它删除按钮,但不替换为图像),则在所有 6+ 版本中都会失败,所以我 使用 HTML 条件过滤流量到适当的脚本,所以整个事情看起来像这样:
<!--[if IE]>
<script>
marker = jQuery('<span class="marker"></span>').insertBefore('input#SearchButton');
new_search = jQuery('<input id="SearchButton" value="Search" name="Searchbutton" type="image" src="'+theme_folder+'/style/images/search_button.png'+'" />')
jQuery(new_search).insertAfter('input#SearchButton');
jQuery('input#SearchButton').remove();
marker.remove();
</script>
<![endif]-->
<![if !IE]>
<script>
marker = jQuery('<span class="marker"></span>').insertBefore('input#SearchButton');
jQuery('input#SearchButton').detach().attr('type', 'image').attr('src',theme_folder+'/style/images/search_button.png').insertAfter(marker);
marker.remove();
</script>
<![endif]>
Problem
The following scripts do exactly what I require, in all browsers tested except IE8, where it removes the button without replacing it with anything.
Background
I have been working on a method to replace submit type inputs with image type inputs using jQuery.
First, I had this following which works with FF and webkit:
marker = jQuery('<span class="marker"></span>').insertBefore('input#SearchButton');
jQuery('input#SearchButton').detach().attr('type', 'image').attr('src',theme_folder+'/style/images/search_button.png').insertAfter(marker);
marker.remove();
But this fails in all 6+ versions if IE (it removes the button, but doesn't replace with image), so I developed the following script for IE browsers:
marker = jQuery('<span class="marker"></span>').insertBefore('input#SearchButton');
new_search = jQuery('<input id="SearchButton" value="Search" name="Searchbutton" type="image" src="'+theme_folder+'/style/images/search_button.png'+'" />')
jQuery(new_search).insertAfter('input#SearchButton');
jQuery('input#SearchButton').remove();
marker.remove();
I am using HTML Conditionals to filter the flow of traffic to the appropriate script, so so the whole thing looks like this:
<!--[if IE]>
<script>
marker = jQuery('<span class="marker"></span>').insertBefore('input#SearchButton');
new_search = jQuery('<input id="SearchButton" value="Search" name="Searchbutton" type="image" src="'+theme_folder+'/style/images/search_button.png'+'" />')
jQuery(new_search).insertAfter('input#SearchButton');
jQuery('input#SearchButton').remove();
marker.remove();
</script>
<![endif]-->
<![if !IE]>
<script>
marker = jQuery('<span class="marker"></span>').insertBefore('input#SearchButton');
jQuery('input#SearchButton').detach().attr('type', 'image').attr('src',theme_folder+'/style/images/search_button.png').insertAfter(marker);
marker.remove();
</script>
<![endif]>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 IE 上,
input
元素的type
属性是一次写入。第一次设置后就无法更改。 (attr
文档中有关于此的警告。)您唯一真实的与 IE 兼容的选项实际上是用具有相同
id
的新元素替换该元素。显然,这会对事件处理程序产生影响(您附加到旧元素的任何事件都不会附加到新元素),因此您可能需要使用delegate
(或全局版本,live
),而不是将处理程序直接附加到相关元素。更新:啊,我看到你已经编写了一些代码来做到这一点。该代码的问题在于,您暂时创建了一个无效文档(它有两个具有相同
id
的不同元素),然后通过重复的id
查找该元素。如果您提前获取该元素就可以了:(您不需要为此标记,因此我已将其从上面删除。)
但是您可能想看看使用
replaceWith
代替:这是一个简化的
replaceWith
示例(还演示了delegate
):HTML:
JavaScript:
Live copy
那里的按钮没有
id
,但如果是的话那就完全没问题:HTML:
JavaScript:
Live copy
离题:由于无论如何您都必须替换该元素才能支持 IE,因此我建议只保留一份代码副本并在所有浏览器上使用它。替换该元素并不那么昂贵,即使在允许您更改它的浏览器上也是如此。
On IE, the
type
property of theinput
element is write-once. You cannot change it once you've set it the first time. (There's a warning about this in theattr
documentation.)Your only real option to be compatible with IE is to actually replace the element with a new one with the same
id
. Obviously this has an impact on event handlers (any you've attached to the old element won't be attached to the new one), so you may want to usedelegate
(or the global version,live
) rather than attaching handlers directly to the element in question.Update: Ah, I see you've written some code to do that. The problem with that code is that you're temporarily creating an invalid document (it has two different elements with the same
id
) and then looking up the element by that duplicateid
. It'll be fine if you just grab the element in advance:(You don't need the marker for this, so I've removed it from the above.)
But you may want to look at using
replaceWith
instead:Here's a simplified
replaceWith
example (also demonstratingdelegate
):HTML:
JavaScript:
Live copy
The button there doesn't have an
id
, but it's totally fine if it does:HTML:
JavaScript:
Live copy
Off-topic: Since you have to replace the element to support IE anyway, I'd recommend just having the one copy of the code and using it on all browsers. It's not that expensive to replace the element, even on browsers that let you change it.
您无法在 Internet Explorer 中动态更改输入元素的类型。
您必须删除旧的输入并添加新的输入
You cannot dynamically change a the type of an input element in Internet Explorer.
You have to remove the old input and add a new input