如果站点的url与预定的url匹配,我需要编写一个jquery选择器来返回DOM中的内容。
作为背景,我正在编写一个专为“单一”站点设计的文档功能,但是我需要将它用于多个站点上的文档。我对服务器或代码没有控制权。我也不能选择另一个框架。
我知道jquery选择器不是为这个而设计的,所以我正在寻找任何有用的东西。
我正在寻找具有下列行为的选择器
在http://someurl/correctpage上说im
然后选择器应该通过$('SELECTORHERE')从DOM返回一些东西。
然而,如果im在http://someurl/falsepage上,它不应该
发布于 2021-12-26 23:09:29
您需要使用哈希
$(function() {
  // we get hash value 
  let hashValue = location.hash;
  hashValue = "correctpage"; // this line is for test only
  // we check if the hash value is egal to your second tab name
  if (hashValue == "correctpage") { 
    $("." + hashValue).removeClass("red").addClass("green");
  }
});.red { border: 1px solid red }
.green { border: 1px solid green }<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="correctpage red">Test</div>
https://stackoverflow.com/questions/70351156
复制相似问题