2011-07-25 17 views
0

現在、ブラウザのURLは "www.xyz.com"で、私のhref = "/ css/home.css"とします。 私の要件は、URLが "www.xyza.com"に変更された場合、または "www.xyzb.com"のように変更された場合、hrefは、href = "/ css/home_a.css "、href ="/css/home_b.css "となります。htmlのブラウザのURLに応じて動的に "href"を変更する方法はありますか?

誰かが私にこれを達成する方法を教えてもらえますか?

+1

何を達成しようとしていますか?例を挙げてください。私はこれを行うことによって何の意味も分かりません – sll

答えて

0

サーバー側のスクリプトを使用して、正しいリンクを生成することができます。あなたはあなたが使っているものについて言及していませんでした。

またはあなたは、ホスト名を見て、JavaScriptでリンクを変更できます。

switch(document.location.host) { 
    case "www.google.com": alert("Google"); break; 
    default: alert("Other!"); 
} 
1

あなたは にdocument.write(document.location.href)で現在位置を取得することができます。あなたのスタイルを作るしようとしている場合 、その後、jQueryを使ってCSSのhrefを変更、

$( "A")。attrの( "HREF"、 "/css/home_a.css")

0

あなたのスタイルスイッチ機能で、あなたのメインページで

<head> 
<!-- bla bla --> 
<link rel="stylesheet" media="screen, projection" type="text/css" id="css" href="<?php echo switch_style();?>" /> 
<head> 

:同じドメイン上のスイッチャー、あなたの特定の質問(私には不明)に適応すること例えばのような簡単なことを行うには2000個の方法があります

function build_css($style) { 
    $css_file='style/'.$style.'/style.css'; // providing here your css path 
    return $css_file; 
} 
function switch_style() { 

    $style_dispo = array('default','style1','style2','style3','style4','style5'); 

    $current_style = ((isset($_SESSION['nav_style'])) AND ($_SESSION['nav_style'] != '')) ? $_SESSION['nav_style'] : 'default'; 

     // There you can adapt the style switch to your need, for example based on the $_SERVER(REQUEST_URI) in your case i/o a GET test case like here 
    $new_style = (isset($_GET['style'])) ? $_GET['style'] : ''; 

    // style change 
    if (($new_style != $current) AND ($new_style !='')) 
    { 
     if (in_array($new_style,$style_dispo)) 
     { 
      $style= $new_style; 
      $_SESSION['nav_style']=$style; 
     } 
     else 
     { 
      $style ='default'; 
      $_SESSION['nav_style']=$style; 
     } 
    } 
    else 
    { 
     $style = $current_style; 
     $_SESSION['nav_style'] = $style; 
    } 
    return build_css($style); 
} 
関連する問題