关于变量参数的问题?

beyoo 2007-04-28 11:51:09
代码如下:
function getObject(objectId) {
if(document.getElementById && document.getElementById(objectId)) {
// W3C DOM
return document.getElementById(objectId);
} else if (document.all && document.all(objectId)) {
// MSIE 4 DOM
return document.all(objectId);
} else if (document.layers && document.layers[objectId]) {
// NN 4 DOM.. note: this won't find nested layers
return document.layers[objectId];
} else {
return false;
}
}
function ShowCss(name){
getObject(name).className="over";
}

不知道错在哪里,效果就是显示不出来。
请高手们指教!谢谢!
...全文
185 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyoo 2007-04-29
  • 打赏
  • 举报
回复
没有重复哟。
yangdengfeng2003 2007-04-29
  • 打赏
  • 举报
回复
看看是不是有ID重复的情况
beyoo 2007-04-29
  • 打赏
  • 举报
回复
<td id="discount" height="40" onclick="ShowCss(this.id)">好</td>
一直都取不到discount这个id
不知道是什么回事?
yixianggao 2007-04-29
  • 打赏
  • 举报
回复
ShowCss(this.id) 是关键!

一切正常,L@_@K


<html>
<style>
.over{
color: red;
font-size:28pt;
}
</style>
<script defer>
function getObject(objectId) {
if(document.getElementById && document.getElementById(objectId)) {
// W3C DOM
return document.getElementById(objectId);
}
else if (document.all && document.all(objectId)) {
// MSIE 4 DOM
return document.all(objectId);
}
else if (document.layers && document.layers[objectId]) {
// NN 4 DOM.. note: this won't find nested layers
return document.layers[objectId];
}
else {
return false;
}
}

function ShowCss(name)
{
// Debug
var o = getObject(name);
alert(o);
alert(o.tagName);
alert(o.className);

getObject(name).className="over";

alert(o.className);
}

</script>

<body>
<div>
<span id="span1" onclick="ShowCss(this.id)">abcd</span>
</div>
</body>
</html>
yixianggao 2007-04-29
  • 打赏
  • 举报
回复
调式一下下!

function ShowCss(name)
{
// Debug
var o = getObject(name);
alert(o);
alert(o.tagName);
alert(o.className);

getObject(name).className="over";

alert(o.className);
}

如果一切正常,但是还没效果的话,就是你的 CSS 类名 over 有问题!!!
beyoo 2007-04-29
  • 打赏
  • 举报
回复
name是变量
function ShowCss(name){
getObject(name).className="over";
}
就是这里的问题了
要实现点击改变样式
function ShowCss(){
getObject('host').className="over";
}
这样是没问题啊。关键就是用了变量就不行了。
duwa789 2007-04-29
  • 打赏
  • 举报
回复
<html>
<style>
.over{
color:red;
text-size:18pt;
}
</style>
<script>
function getObject(objectId) {
if(document.getElementById && document.getElementById(objectId)) {
// W3C DOM
return document.getElementById(objectId);
}
else if (document.all && document.all(objectId)) {
// MSIE 4 DOM
return document.all(objectId);
}
else if (document.layers && document.layers[objectId]) {
// NN 4 DOM.. note: this won't find nested layers
return document.layers[objectId];
}
else {
return false;
}
}

window.onload = function() {
getObject('span1').className = 'over';
}
</script>

<body>
<div>
<span id="span1">abcd</span>
</div>
</body>
</html>

没啥问题啊, 楼主是不是没有定义over样式啊
又或者name不存在?
mingxuan3000 2007-04-29
  • 打赏
  • 举报
回复
这里面没什么效果,要显示什么?

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧