IE6/7 和 ClassName (JS/HTML)

发布于 2024-07-18 20:12:17 字数 1787 浏览 7 评论 0原文

我正在尝试使用 javascript 更改元素的类。 到目前为止,我正在做:

var link = document.getElementById("play_link");
link.className = "play_button";

编辑:这是替换类名的实际代码

在 HTML 中:

<a href="#play_link_<%=i%>" id="play_link_<%=i%>"onclick="changeCurrentTo(<%=i%>);return false;" class="play_button"></a>

在 Javascript 中 函数changeCurrentTo(id){ activatePlayButton(current_track); 当前轨道=id; inactivatePlayButton(current_track); 上下文

function inactivatePlayButton(id){
    document.getElementById("recording_"+id).style.backgroundColor="#F7F2D1";
    var link = document.getElementById("play_link_"+id);
    link.className="stop_button";
    link.onclick = function(){stopPlaying();return false;};
}

function activatePlayButton(id){
    document.getElementById("recording_"+id).style.backgroundColor="";
    var link = document.getElementById("play_link_"+id);
    link.className = "play_button";
    var temp = id;
    link.onclick = function(){changeCurrentTo(temp);return false;};
}

.play_button{
    background:url(/images/small_play_button.png) no-repeat;
    width:25px;
    height:24px;
    display:block;
}

类是

.stop_button{
    background:url(/images/small_stop_button.png) no-repeat;
    width:25px;
    height:24px;
    display:block;
}

是音乐播放器。 当您单击播放按钮(三角形)时,它会变成停止按钮(方形)并替换所调用的函数。

问题是类已更改,但在 IE6 和 7 中,新背景(此处为 /images/small_play_button.png)不会立即显示。 有时甚至根本不显示。 有时它不显示,但如果我稍微摇动鼠标,它就会显示。

它在 FF、Chrome、Opera 和 Safari 中完美运行,所以这是 IE 的 bug。 我知道仅凭这些信息很难立即判断,但如果我能得到一些指示和方向,那将会有所帮助。

谢谢-

I am trying to change the class of an element using javascript.
So far I'm doing :

var link = document.getElementById("play_link");
link.className = "play_button";

edit: here is the actual code that replace the classname

In the HTML :

<a href="#play_link_<%=i%>" id="play_link_<%=i%>"onclick="changeCurrentTo(<%=i%>);return false;" class="play_button"></a>

In the Javascript
function changeCurrentTo(id){
activatePlayButton(current_track);
current_track = id;
inactivatePlayButton(current_track);
}

function inactivatePlayButton(id){
    document.getElementById("recording_"+id).style.backgroundColor="#F7F2D1";
    var link = document.getElementById("play_link_"+id);
    link.className="stop_button";
    link.onclick = function(){stopPlaying();return false;};
}

function activatePlayButton(id){
    document.getElementById("recording_"+id).style.backgroundColor="";
    var link = document.getElementById("play_link_"+id);
    link.className = "play_button";
    var temp = id;
    link.onclick = function(){changeCurrentTo(temp);return false;};
}

with

.play_button{
    background:url(/images/small_play_button.png) no-repeat;
    width:25px;
    height:24px;
    display:block;
}

the old class is

.stop_button{
    background:url(/images/small_stop_button.png) no-repeat;
    width:25px;
    height:24px;
    display:block;
}

The context is a music player. When you click the play button (triangle) it turns into a stop button (square) and replace the function that is called.

The problem is that the class get changed, but in IE6 and 7 the new background (here /images/small_play_button.png) does not display right away. Sometime it doesn't even display at all. Sometime it doesn't display but if I shake the mouse a bit then it displays.

It works perfectly in FF, Chrome, Opera and Safari, so it's an IE bug. I know it's hard to tell right away from only these information, but if I could get some pointers and directions that would be helpful.

Thanks-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗼ふ静 2024-07-25 20:12:17

您应该创建宽度为 50px 和高度为 24px一个图像,其中包含播放部分和停止部分。 然后,您只需像这样调整背景位置:


.button
{
    background-image: url(/images/small_buttons.png);
    bacground-repeat: no-repeat;
    width: 25px;
    height: 24px;
    display: block;
}

.play_button
{
    background-position: left top;
}

.stop_button
{
    background-position: right top;
}

然后同时加载“两个图像”,当您更改显示图像的哪一部分时,不会发生延迟。

请注意,我创建了一个新的 CSS 类,以便您无需为不同的按钮重复 CSS。 您现在需要在元素上应用两个类。 例子:

<div class="button play_button"></div>

You should create one image with a width of 50px and a height of 24px where you have both the play part and the stop part. Then you just ajust the background position like this:


.button
{
    background-image: url(/images/small_buttons.png);
    bacground-repeat: no-repeat;
    width: 25px;
    height: 24px;
    display: block;
}

.play_button
{
    background-position: left top;
}

.stop_button
{
    background-position: right top;
}

Then you load "both images" at the same time, and no delay will happen when you change which part of the image gets displayed.

Note that I have made a new CSS class so that you dont need to repeat your CSS for different buttons. You now need to apply two classes on your element. Example:

<div class="button play_button"></div>
江城子 2024-07-25 20:12:17

您需要在两个函数中使用 setAttribute 。 试试这个:
link.setAttribute((document.all ? "className" : "class"), "play_button");
link.setAttribute((document.all ? "className" : "class"), "stop_button");

You need to use setAttribute in your two funcitons. Try this out:
link.setAttribute((document.all ? "className" : "class"), "play_button");
link.setAttribute((document.all ? "className" : "class"), "stop_button");

泅人 2024-07-25 20:12:17

如果没有看到确切的标记,很难说,但 IE 中消失的背景图像问题可能可以通过向 .button 类添加 position:relative; 声明来解决, /或者到它的父div

Without seeing the exact markup, it's difficult to say, but the disappearing background image issue in IE is probably solved by adding a position: relative; declaration to the .button class and/or to its parent div.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文