如果更改页面,请更改标记<h1>

时间:2018-01-17 07:19:18

标签: javascript php html

我有一些JavaScript代码,如果主页或索引(例如malaspulang.com)标记<h1>没有变化。但是,如果我更改了网页(例如malaspulang.com/lifestyle),则标记<h1>将替换为<h2>

这是我的代码:

<div id="header" class="hide"><h1>this my title</h1></div>
<script>
if(window.URL === 'malaspulang.com'){

}else{

 $('#header').empty();
 $('#header').html('<h2>this my title</h2>');
}
</script>

在我的首页网站中,标记<h1>应替换为<h2>我认为我的代码是真的,但我对此代码感到困惑。所以,如果有人可以帮助我,我会很高兴:)

注意:如果有人用PHP编写代码,你可以告诉我:)

7 个答案:

答案 0 :(得分:2)

也许是这样的:

<div class="hide" id="header">
  <h1>this my title</h1>
</div>
<script>
   $(document).ready(function(){
      console.log(document.URL);//this optional , just display your link at now
      var uri= document.URL;
      if(uri == 'http://malaspulang.com/'){ //use full link from your site
      //do nothing , because logical true
      }else{
          $('#header').empty();
          $('#header').html('<h2>this my title</h2>');
      }
   });
</script>

希望这能帮到你

答案 1 :(得分:0)

使用#header

访问ID
trial_search = RandomizedSearchCV( estimator=keras_model, ... )
pickle.dump( trial_search, open( "trial_search.pickle", "wb" ) )

答案 2 :(得分:0)

$('.header')替换为$('#header')

$('#header') - 定位元素 id 属性'header'。

$('.header') - 使用属性'标题'定位元素。

答案 3 :(得分:0)

您的问题是element 已定义class。如下所示:<div id="header"><h1>this my title</h1></div>,它的标题id不是标题的class。这意味着您的JavaScript需要调用id的“标题”,而不是class

以下是可行的代码:

$('#header').html('<h2>This is my title</h2>');

答案 4 :(得分:0)

而不是使用window.URL尝试使用window.location.href并编写完整的网址名称:http://malaspulang.com

<div id="header" class="hide">
  <h1>this my title</h1>
</div>
<script>
if (window.location.href === 'https://fiddle.jshell.net/_display/') { // current url
    document.getElementById('header').innerHTML = "";
    document.getElementById('header').innerHTML = "<h1>this my title (h1)</h1>";
}
else {
    document.getElementById('header').innerHTML = "";
    document.getElementById('header').innerHTML = "<h2>this my title (h2)</h2>";
}
</script>

您也可以在jsfiddle

上试用

答案 5 :(得分:0)

if($("body").hasClass("home")) {
    // don't do anything.
} else{
    // fire your code.
}

答案 6 :(得分:-1)

使用$('#header')代替$('.header')(由id选择,而不是class选择)。而且您不需要$('.header').empty()