2013-08-03 4 views
0

문제가 있습니다. 내 영역 모양 href = "/ kosmetikstudios/deutschland/Bayern"태그가 그렇게 보입니다. 그리고 나는 매개 변수 "Bayern"을 사용할 것입니다 (이것은 URL의 마지막 매개 변수입니다).URL의 매개 변수를 사용하는 자바 스크립트

나는이 동적이 필요합니다. 여기

<script type="text/javascript"> 
    $(document).ready(function(){ 
    var mName="passiv"; 
    $("#map area").mouseenter(function(){ 

     mName = $(this).attr("href"); // i don't have idea to implement this 
     bName="images/" +mName + ".png"; 
     $("#map img").attr("src",bName); 

    }); 

    $("#map area").mouseleave(function(){ 
     bName="images/Europa.png"; 
     $("#map img").attr("src",bName); 

    }); 

    }); 
</script> 

내 HTML 코드 나는 누군가가 나를 도울 수 있기를 바랍니다

<area shape="poly" coords="356,300,355,299,354,298....." href="/kosmetikstudios/deutschland/Bayern" title="Kosmetikstudios in Bayern" alt="Kosmetikstudios in Bayern" /> 

<area shape="poly" coords="156,200,425,699,154,298....." href="/kosmetikstudios/deutschland/Berlin" title="Kosmetikstudios in Berlin" alt="Kosmetikstudios in Bayern" /> 

있습니다 : 여기

내 자바 스크립트 코드입니다. 당신은이 작업을 수행 할 수

감사합니다,

데이브

답변

1
mName = this.href.substring(
    this.href.lastIndexOf('/') + 1 
); 
+1

당신은 간단하게 사용할 수는 'this.href'는 jQuery를 개체는 필요하지 않습니다. – bfavaretto

+1

예. 그렇지. 그렇지. :-) – Vinay

0

:

mName = $(this).attr("href").split("/").reverse()[0]; 
+0

'this.href.split ("/")을 고려하십시오. reverse() [0];' – bfavaretto

+0

예, of native JS가 더 좋습니다. – Niels

1

의 URL에서 값을 추출하는 당신이 이름을 저장하는 data attribute을 설정할 수 있습니다 대신에. 예를 들어

:

HTML

<area shape="poly" data-name="Bayern" href="/kosmetikstudios/deutschland/Bayern" /> 
<area shape="poly" data-name="Berlin" href="/kosmetikstudios/deutschland/Berlin" /> 

JS

<script type="text/javascript"> 
    $(document).ready(function(){ 
    var mName="passiv"; 
    $("#map area").mouseenter(function(){ 

     //read the data attributes 
     mName = $(this).data("name"); 

     bName="images/" +mName + ".png"; 
     $("#map img").attr("src", bName); 

    }); 

    $("#map area").mouseleave(function(){ 
     bName="images/Europa.png"; 
     $("#map img").attr("src",bName); 

    }); 

    }); 
</script> 
+0

오 예. 그것은 놀랍습니다. 고맙습니다. –

+0

차가움. 그것이 도움이된다면 당신이 대답으로 upvote 또는 받아 들일 수 있다면 그것은 최고가 되십시오. 고마워! –

관련 문제