JQuery根据页面url / name更改具有id或class的元素类

时间:2016-03-09 06:46:47

标签: javascript jquery

这是我的代码

HTML

FC      = ifort
FFLAGS  = -c -free -module modules -g3 -warn all -warn nounused
LDFLAGS = -save-temps -dynamiclib

INTERFACES = src/Foundation.f units/UFoundation.f units/Asserts.f units/Report.f
EXCLUDES   = $(patsubst %, ! -path './%', $(INTERFACES))
SOURCES    = $(INTERFACES) \
             $(shell find . -name '*.f' $(EXCLUDES) | sed 's/^\.\///' | sort)
OBJECTS    = $(patsubst %.f, out/%.o, $(SOURCES))
EXECUTABLE = UFoundation

all: $(SOURCES) $(EXECUTABLE)

release: SOURCES := $(filter-out units/%.f, $(SOURCES))
release: OBJECTS := $(filter-out units/%.o, $(OBJECTS))
release: EXECUTABLE := 'Foundation.dlyb'
release: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    @echo 'Linking to $@...'
    @$(FC) $(LDFLAGS) $(OBJECTS) -o out/$@

out/%.o: %.f
    @echo 'Compiling $@...'
    @mkdir -p modules
    @mkdir -p $(dir $@)
    @$(FC) $(FFLAGS) -c $< -o $@

clean:
    @echo "Cleaning..."
    @rm -rf modules out $(EXECUTABLE)

Jquery的

<div id="header-content" class="header-content">...</div>

网址示例: http://www.somedomain.com/the-page/

我想要做的是让脚本在网址中标识文本“the-page”,如果是匹配则分配该类。正如您在我的示例jquery代码中看到的那样,我正在尝试将类分配给主页,start-here页面和with-me-me页面。

我不确定如何修改上面的Jquery,因此它将使用URL Example格式。

我如何检测它是否是索引页面,URL如下所示:http://www.somedomain.com/结尾没有页面名称?

1 个答案:

答案 0 :(得分:1)

好吧,假设域已知,它应该是可行的:

$( document ).ready(function() {

  var loc = window.location.href; // returns the full URL
  var root = "somedomain.com";
  var end = loc.slice(loc.lastIndexOf(root)+root.length);

  if(end.length <= 0) { // If Empty or if just home URL
    $('#header-content').addClass('home-page');
    //Remove All Other Classes
    $('#header-content').removeClass('start-here');
    $('#header-content').removeClass('work-with-me');
  }
  if(end === "/start-here/") { // if page = root/start-here/
    $('#header-content').addClass('start-here');
    //Remove All Other Classes
    $('#header-content').removeClass('home-page');
    $('#header-content').removeClass('work-with-me');
  }
  if(end === "/work-with-me/") { // if page = root/work-with-me/
    $('#header-content').addClass('work-with-me');
    //Remove All Other Classes
    $('#header-content').removeClass('home-page');
    $('#header-content').removeClass('start-here');
  }


});

其中,使用您的示例看起来像:

{{1}}