2016-07-14 3 views
-1

URLに渡されるクエリパラメータに応じて、ページの見出しタグを変更するためにjQueryを使用しようとしています。次URLに基​​づいてページ見出しを変更する方法

http://example.com/ 
http://example.com?type=friends 
http://example.com?type=honeymoon 
http://example.com?type=family 

は、私は隠し、渡されたクエリに基づいて表示されます見出しタグです:次のように

可能なURLパラメータがある可能性があります。

<h2 id="default">Bali Vacations</h2> 
<h2 id="friends">Friends</h2> 
<h2 id="honeymoon">Honeymoon</h2> 
<h2 id="family">Family</h2> 

私は、ユーザーの訪問のみドメインがまたは上記のものとは別に、他のクエリパラメータに行くときだけ<h2 id="default"></h2>を表示したいと思います。

あなたが私を助けることができるなら、それはたくさんいただきます。ありがとうございました。

UPDATE

は、これを行う方法を見つけ出すことができました。これは最も理想的な解決策ですか?

$(document).ready(function() { 
     // If no URL Query Param 
     if(location.search == ""){ 
      $('.default_name').show(); 
     } 
     // If honeymoon is query param 
     else if(location.search == "?type=honeymoon"){ 
      $('#honeymoon').show(); 
     } 
     // If family is query param 
     else if(location.search == "?type=family"){ 
      $('#family').show(); 
     } 
     // If friends is query param 
     else if(location.search == "?type=friends"){ 
      $('#friends').show(); 
     } 
     // Any other query param 
     else { 
      $('.default_name').show(); 
     } 

    }); 
+0

)既存のコードを表示 – ADyson

+0

@Justinas - 自分のコードを先に更新しましたか?方法?ありがとう –

答えて

0
var url = window.location.href; //get the url 
var param = url.split('=')[1]; //split the url and get the parameter 
$('#default').text(param) //set the text to url parameter 
$('#default').attr('id',param); //now change the id from default to param 

私はjqueryのを使用して、必要なコードを書きました!可能な異なる条件を使用する(例:IDがファミリの場合はIDを取得し、それに応じてコードを

関連する問題