Javascript根据浏览器的ID隐藏元素
我正在尝试向所有浏览器显示一条消息,并通过 javascript 将其删除(如果支持)。目前隐藏它的唯一方法是单击隐藏按钮。它不会在我的 if 语句中运行。
<html>
<head>
<script type="text/javascript">
var browser=navigator.appCodeName + navigator.appName;
function hideStuff(id) {
document.getElementById(id).style.display = 'none';
}
function supportedBrowser() {
if (browser == "MozillaNetscape")
{
document.write("Browser is Mozilla Netscape");
}
else if (browser == "MozillaOpera")
{
document.write("Browser is Mozilla Opera");
hideStuff('browserMessage');
}
else
{
alert("Please upgrade your browser.");
}
}
//-->
</script>
</head>
<body>
<a href="#" onclick="hideStuff('browserMessage'); return false;">Hide</a>
<br><br>
<p id="browserMessage" style="display: inline;">Please use a supported browser.</p>
</body>
</html>
根据“bluevoodoo1”进行更改
<html>
<head>
<script type="text/javascript">
var browser=navigator.appCodeName + navigator.appName;
function hideStuff(id) {
document.getElementById(id).style.display = 'none';
}
function supportedBrowser() {
if (browser == "MozillaNetscape")
{
document.write("Browser is Mozilla Netscape");
}
else if (browser == "MozillaOpera")
{
document.write("Browser is Mozilla Opera");
//hideStuff('browserMessage');
}
else
{
alert("Please upgrade your browser.");
}
}
function init() {
supportedBrowser();
//etc
}
window.onload = supportedBrowser;
//-->
</script>
</head>
<body>
<a href="#" onclick="hideStuff('browserMessage'); return false;">Hide</a>
<br><br>
<p id="browserMessage" style="display: inline;">Please use a supported browser.</p>
<p>This text shoudld not be hidden.</p>
</body>
</html>
有人知道我做错了什么吗?
I am trying to show a message to all browsers and remove it though javascript if it is supported. Currently the only way it will be hidden is if I click a hide button. It won't run in my if statement.
<html>
<head>
<script type="text/javascript">
var browser=navigator.appCodeName + navigator.appName;
function hideStuff(id) {
document.getElementById(id).style.display = 'none';
}
function supportedBrowser() {
if (browser == "MozillaNetscape")
{
document.write("Browser is Mozilla Netscape");
}
else if (browser == "MozillaOpera")
{
document.write("Browser is Mozilla Opera");
hideStuff('browserMessage');
}
else
{
alert("Please upgrade your browser.");
}
}
//-->
</script>
</head>
<body>
<a href="#" onclick="hideStuff('browserMessage'); return false;">Hide</a>
<br><br>
<p id="browserMessage" style="display: inline;">Please use a supported browser.</p>
</body>
</html>
Changed as according to "bluevoodoo1"
<html>
<head>
<script type="text/javascript">
var browser=navigator.appCodeName + navigator.appName;
function hideStuff(id) {
document.getElementById(id).style.display = 'none';
}
function supportedBrowser() {
if (browser == "MozillaNetscape")
{
document.write("Browser is Mozilla Netscape");
}
else if (browser == "MozillaOpera")
{
document.write("Browser is Mozilla Opera");
//hideStuff('browserMessage');
}
else
{
alert("Please upgrade your browser.");
}
}
function init() {
supportedBrowser();
//etc
}
window.onload = supportedBrowser;
//-->
</script>
</head>
<body>
<a href="#" onclick="hideStuff('browserMessage'); return false;">Hide</a>
<br><br>
<p id="browserMessage" style="display: inline;">Please use a supported browser.</p>
<p>This text shoudld not be hidden.</p>
</body>
</html>
Does anyone know what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可能需要使用 window.onload 处理程序...(jquery 的“document.ready”可能是首选,但我在下面使用一些本机 javascript)
you might need use the window.onload handler... (jquery's "document.ready" is probably preferred, but I'm using some native javascript below)