如何获得经典的悬停、活动等。 JavaScript 的行为

发布于 2025-01-03 07:23:08 字数 461 浏览 3 评论 0原文

我一直遇到这个简单的问题。现在我总是使用相同的解决方案,但我想这不是正确的方法。

情况如下:

如果有一个包含 6 个链接和 6 个选项卡的页面。每个链接都使用 javascript 显示或隐藏一个选项卡 (display:block/none)。我遇到了正常的 html/css 方式不起作用的问题。我可以设置我的CSS来使链接悬停,但诸如活动等之类的东西不起作用。现在我使用 6 个单独的函数来操纵每个链接的样式,以便当人们单击链接 5 时,链接 5 会带有下划线,而其余链接则不会。

我已经尝试使用 .thisobj. 重构我的代码,但这仍然没有给出我想要的行为。貌似用js改了css之后css就不行了。

我无法想象这是实现正常 html 链接行为的正确方法?

有人有想法吗?你如何解决这个问题?对于我的新项目来说,这成为一个真正的问题,因为所有内容都是自动生成的。

I have this simple problem that I keep running into. Now I always use the same solution but I guess it is not the right way of doing it.

Here is the case:

If have a page with 6 links and 6 tabs. Each link shows or hide a tab with javascript (display:block/none). I run into the problem that the normal html/css way doesn't work. I can set my css to make a link hover, but things like active, etc. don't work. Now i use 6 separate functions to manipulate the style of each link so that when people click link 5, link 5 is underlined and the rest not.

I already tried to refactor my code by using .this and obj. but this still doesn't give the behavior I want. It seems that the css doesn't work anymore once you change the css with js.

I can't imagine that this is the correct way to achieve the normal html link behavior?

Does anybody have ideas? How do you fix this problem? This becomes a real problem for my newer projects where all the content is automatically generated.

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

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

发布评论

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

评论(3

笑饮青盏花 2025-01-10 07:23:08

一旦你用js改变CSS,CSS似乎就不再起作用了。

这表明(直接通过一些抽象)您正在使用 JS 来更改元素的 .style.* 属性。

这些映射到 style 属性,该属性始终被认为是“最具体”。

解决方案是保留 style 属性。如果您想更改元素的样式,请在样式表中预先编写 CSS。。然后使用 JavaScript 修改元素的 className

这使您可以更好地控制所应用的样式。

(这只是遵循关注点分离的通常规则。开发人员通常越来越擅长将 CSS 与 HTML 分开,将 JS 与 HTML 分开,但最常犯的错误是未能将 CSS 与 JS 分开)

It seems that the css doesn't work anymore once u change the css with js.

This suggests that (directly of via some abstraction) you are using JS to change the .style.* properties of the element.

These map on to the style attribute, which is always considered to be "most specific".

The solution is to leave the style attribute alone. If you want to change the style of an element then have your CSS prewritten in the stylesheet. Then use JavaScript to modify the className of the element.

This gives you better control over what styles are being applied.

(This just follows on from the usual rules about separation of concerns. Developers are generally getting better at keeping the CSS separate from the HTML and the JS separate from the HTML, but slip up most often in failing to keep the CSS separate from the JS)

以可爱出名 2025-01-10 07:23:08

如果使用jQuery,则可以使用hover 功能轻松创建hover 状态效果。

if ($("#tab").hasClass('active') {
    $(".class-of-item-to-show").show();
}

$("#tab1").hover(
  function () {
    $(".class-of-item-to-show").show();
  }, 
  function () {
    $(".class-of-item-to-show").hide();
  }
);

如果您发布要在问题中显示的选项卡和数据,我可以帮助您将其设置为循环。

If you use jQuery, you can use the hover function to easily create a hover state effect.

if ($("#tab").hasClass('active') {
    $(".class-of-item-to-show").show();
}

$("#tab1").hover(
  function () {
    $(".class-of-item-to-show").show();
  }, 
  function () {
    $(".class-of-item-to-show").hide();
  }
);

If you post your tabs and data to display in the question I can help you set it up as a loop..

滥情哥ㄟ 2025-01-10 07:23:08

我认为你可以用 jQuery 实现你想要的功能。

我只会提供一个示例代码,以便您可以有一个想法。对于以下一组链接。

<a href ="#" class ="test not" >a</a>
<a href ="#" class ="test not" >b</a>
<a href ="#" class ="test not" >c</a>

然后使用 jQuery live,您可以为任何事件编写代码,例如,我将展示如何在悬停事件中删除和添加类,

<script type ="text/javascript">
    $(document).ready(function () {
        $('.test').live('hover', function () {

            $(this).removeClass('not');
            $(this).addClass('selected');                     

        });
    });
</script>

如上面的代码,您可以单独识别选定的链接。您也可以使用任何其他事件作为“悬停”

希望这有帮助

I think you can achieve the functionality you want with jQuery.

I will just put a sample code so that you can have an idea. For the following set of links.

<a href ="#" class ="test not" >a</a>
<a href ="#" class ="test not" >b</a>
<a href ="#" class ="test not" >c</a>

then using jQuery live you can write code for any event for example I will show how to remove and add classes in hover event

<script type ="text/javascript">
    $(document).ready(function () {
        $('.test').live('hover', function () {

            $(this).removeClass('not');
            $(this).addClass('selected');                     

        });
    });
</script>

like the above code you can identify links with selected separately. You can use any other event as 'hover' as well

hope this helps

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