Javascript 有没有一个js函数可以让占位符属性在IE中工作?
现在我的项目中有一堆使用占位符的输入标记,如下所示:Javascript 有没有一个js函数可以让占位符属性在IE中工作?,javascript,jquery,html,internet-explorer,placeholder,Javascript,Jquery,Html,Internet Explorer,Placeholder,现在我的项目中有一堆使用占位符的输入标记,如下所示: <input id="Name" name="Name" placeholder="Name Goes Here" type="text" value=""> 如果浏览器是IE,我是否可以在我的全局js脚本中放置一个js函数来更改输入标记 例如,如果浏览器是internet explorer,我可以运行一个特定的javascript函数,将我的所有占位符更改为IE使用的内容(如果有的话)请检查//根据需要检测浏览器。我在用下
<input id="Name" name="Name" placeholder="Name Goes Here" type="text" value="">
如果浏览器是IE,我是否可以在我的全局js脚本中放置一个js函数来更改输入标记
例如,如果浏览器是internet explorer,我可以运行一个特定的javascript函数,将我的所有占位符更改为IE使用的内容(如果有的话)请检查//根据需要检测浏览器。我在用下面的方法
var browserName=navigator.appName;
如果(浏览器名==“Microsoft Internet Explorer”)
{
更换支架();
}
//用简单的文本输入替换所有占位符
函数replacePlHolders()
{
变量plInps=$(“输入[占位符]”);
plInps.each(函数(){
var name=$(this.attr(“name”);
var newInput=$(“”);
$(this).replaceWith(newInput);
var defaultValue=name+“转到此处”;
newInput.on('focus',function(){
//如果这个输入值等于我们的样本,
//当用户单击它时隐藏它。
if(this.value==默认值)
这个值=“”;
});
newInput.on('blur',function(){
//当他们单击关闭输入时,如果
//该值为空,请带回样本。
如果(this.value=='')
this.value=默认值;
});
});
}
将此代码放在全局Javascript文件中,这将为您带来奇迹
检查小提琴IE10是否支持占位符属性。在底层版本中,您需要使用falinsky建议的垫片。这非常有效。我使用了您给我的插件,它可以正常工作。但是,当我导航到另一个页面时,它会停止工作。有没有一种方法可以让我点击的每个页面都运行这个插件?谢谢你应该在每个页面上都包含这个插件。。。
if(!Modernizr.input.placeholder){
$('[placeholder]').focus(function(){
var input = $(this);
if(input.val() == input.attr('placeholder')){
input.val('');
input.removeClass('placeholder');
}
}).blur(function(){
var input = $(this);
if(input.val() == '' || input.val() == input.attr('placeholder')){
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
$('[placeholder]').parents('form').submit(function(){
$(this).find('[placeholder]').each(function(){
var input = $(this);
if(input.val() == input.attr('placeholder')){
input.val('');
}
});
});
}
// Detect the browser, as you want. I'm using the follwowing way
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer")
{
replacePlHolders();
}
// replace all the placeholders with a simple text input
function replacePlHolders()
{
var plInps = $("input[placeholder]");
plInps.each(function(){
var name=$(this).attr("name");
var newInput = $("<input type='text' name='"+name+"' value='"+name+" goes here'>");
$(this).replaceWith(newInput);
var defaultValue = name + " goes here";
newInput.on('focus', function() {
// If this value of the input equals our sample,
// hide it when the user clicks on it.
if(this.value === defaultValue)
this.value = '';
});
newInput.on('blur', function() {
// When they click off of the input, if
// the value is blank, bring back the sample.
if(this.value === '')
this.value = defaultValue;
});
});
}