我想判断浏览器是否支持input的placeholder属性,进而决定是否用js添加相应功能。我在网上看到了下面代码,为什么弹出的总是‘no’啊?有些浏览器明明支持placeholder的,将属性名换成name也是如此
<input type='text' placeholder="用户名"/>
<script>
function x(){
var element = document.getElementsByTagName('input')[0];
if('placeholder' in element.style)
{alert('yes');}
else
{alert('no');};
};
x();
</script>
如果这个办法不合理,还有别的办法实现吗?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
style是指css里的属性,placeholder是标签的属性,可以用element.hasAttribute或直接if(“placeholder” in element)