【问题标题】:grab value of URL segment and update a class accordingly?获取 URL 段的值并相应地更新一个类?
【发布时间】:2012-02-15 20:04:36
【问题描述】:

我正在 jQuery 中构建一个多步骤表单,它会根据当前显示的表单片段更新 URL。

例如:

  • index.php#form=step1
  • index.php#form=step2
  • index.php#form=step3

我还构建了一个链接到每个这些片段的面包屑菜单,但我需要以某种方式向当前显示的面包屑项目添加一个“活动”类。不知道我应该怎么做,但我想可能会找出 #form 的值是什么,如果该值与面包屑项的 id 匹配,然后将其类更新为活动?

例如:

if form = step1 
breadcrumbItem addClass "active"

if form = step2 
breadcrumbItem addClass "active"

etc

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    我建议使用:

    var url = document.location.href;
    var step;
    if (url.indexOf('=') > -1){
        step = url.split('=')[1];
    }
    
    document.getElementById(step).className = 'active';
    

    JS Fiddle demonstration of concept.

    以上假设面包屑项有ids如step1step2等。

    另外在演示中,我明确地将字符串分配给变量url,这是因为 JS Fiddle 的工作方式。在您的页面中使用,如上面的代码,var url = document.location.href

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      相关资源
      最近更新 更多