2011-01-25 2 views
-1

이 링크가 원하는 함수를 호출할까요?<a>을 사용하여 함수()를 실행하십시오.

<a href="http://catalog.bitsltd.us/power_strips#replaced" onclick="document.getElementById('caShip').function caShip()" id="caShip">Some Text</a> 

이것은 내가 전화하고 싶은 기능입니다. 아무도 왜 작동하지 않는지 말할 수 있습니까?

<script type="text/javascript"> 
$(function caShip(){ 
    $('#caShip').replaceWith('Some HTML (the new HTML has an id of replaced)'); 
}); 
</script> 

링크는 같은 페이지와 ID 인 HREF로 이동 않습니다 클릭하면, 그러나 그것은 < DIV>는 새로운 HTML로>는 <를 대체하지 않습니다?

업데이트 :

JS

<script type="text/javascript"> 
$(document).ready(function(){ 
    //Hide Canadian Information 
    $('.locationDiv').hide(); 

    //bind the links click events 
    $('.locLink').click(function(){  
     $('#1').hide(); 
     $('#desc_' + $(this).attr('title')).show(); 
    }); 
});  

</script> 

HTML

<a href="javascript:void(0);" title="can" class="locLink" id="1">Canadian Customers Click Here Before Ordering!</a> 
<div id="desc_can" class="locationDiv"> 
    <table class="contentpaneopen"> 
     <tr> 
     <td class="contentheading" width="100%">Attention Canadian Customers! 
     </td> 
     </tr> 
    </table> 

    <table class="contentpaneopen"> 
     <tr> 
     <td valign="top" > 
     <span class="body">Please note that there are fees associated with shipping to Canada from the US that are <b><u><i><font color="red">NOT</font></i></u></b> included in the cost of the shipping or the cost of the unit. These cost are to be paid for by the purchaser. Here are some tips for shipping to Canada: 
     <br /> 
     <br /> 
     -USPS methods are cheap but very unreliable. <b>Border fees</b> are not charged using USPS, only UPS or Fed Ex (which are the most reliable). 
     <br /> 
     -<b>Customs fees</b> can sometime run <b>up to 50%</b> of the purchase price (UPS/FedEx). 
     <br /> 
     -Smart Strips are available from a Canadian dealer. Visit our <a href="index.php?Itemid=146" title="Store Locator" target="_blank">Store Locator</a> to find a local seller. 
     <br /> 
     -Customers with a UPS or FedEx account may ship on their account and assume all fees with no delays. 
     <br /> 
     -Canadian customers selecting UPS or FedEx will have to pick the package up at their local station and pay the fees. So you order it online, but still have to drive and pay to pick it up unless you used your own UPS/Fed Ex account.</span> 
     </td> 
     </tr> 
    </table> 
</div> 

내가없이 필요한 것을 얻을 수 있기 때문에 내가 CSS를 사용하지 않은 :이 작업 코드입니다. 이걸 도와 줘서 고마워! !!!

답변

0

이 시도 ...

HTML

<a href="#replaced" id="caShip">Some Text</a> 

JS

:

이 작업을 수행하는 올바른 방법은 사용 click 처리기를 추가 jQuery를하다

$('#caShip').click(function(){ 
    $(this).text('Some HTML (the new HTML has an id of replaced)'); 
}); 

+0

나는 이것에 조금 늦었다. 하아. – Dutchie432

+0

페이지가 기본 URL이 현재 페이지와 다르기 때문에 href로 '#replaced'를 사용할 수 없다는 점을 제외하고는이 작업을 정확하게 수행합니다. 나는 행운을 빌어 모든 사람들의 제안을 시험해 보았다. 페이지를 다시로드하지만 을 js의 HTML로 바꾸지 않습니다. – MJ3111

+0

그것은 페이지를 다시로드 할 때 클라이언트 측이므로 JS로하는 작업은 모두 재설정되기 때문입니다. – Dutchie432

1
<a href="http://catalog.bitsltd.us/power_strips#replaced" onclick="caShipFunc();" id="caShip">Some Text</a> 


<script type="text/javascript"> 
    function caShipFunc(){ 
     $('#caShip').replaceWith('Some HTML (the new HTML has an id of replaced)'); 
    } 
</script> 
4

번호

인라인 onclick 속성을 제거하고, 그냥 이렇게하기 : 인라인 그것을 원하지 않은 경우

<script type="text/javascript"> 
$(function(){ 
    $('#caShip').click(function() { 
     $(this).replaceWith('<div id="' + this.hash.slice(1) + '">some HTML</div>'); 
      // uncomment the next line if you want the hash to actually appear. 
     // window.location.hash = this.hash; 

      // prevent the page from reloading 
     return false; 
    }); 
}); 
</script> 

, 당신은 단지이 작업을 수행 할 수 있습니다 :

<a href="http://catalog.bitsltd.us/power_strips#replaced" onclick="caShip.call(this);" id="caShip">Some Text</a> 


<script type="text/javascript"> 
function caShip(){ 
    $(this).replaceWith('<div id="' + this.hash.slice(1) + '">some HTML</div>'); 
} 
</script> 

EDIT :대체에 사용 된 텍스트가 의미하는 것처럼 보이기 때문에 Anchor 요소의 hash을 사용하여 해당 ID로 새 요소를 만들 수있는 문제를 해결했습니다. 당신이 jQuery를 사용하고 있기 때문에

+0

이 경우 꼭가는 길입니다. 그러나, 항상 함수를 호출하는 요소의 id를 알기를 원하지는 않습니다. 아니면 디자인에 의한 것일 수도 있습니다. 두 가지 방법 모두 잘 작동하며이 예제에서는 jquery click()이 멋지다고 생각합니다! – stefan

+0

가 작동하지 않는 것 같습니다. 페이지는 다시로드되지만 변경되지 않습니다. 이전 HTML은 여전히 ​​페이지에 있습니다. – MJ3111

1

, 당신은 같이해야합니다 :

<a href="http://catalog.bitsltd.us/power_strips#replaced" id="caShip">Some Text</a> 

jQuery를에게

<script type="text/javascript"> 
$(function(){  
    $('#caShip').click(function() { 
     if($(this).attr('href').indexOf("#") != -1) { 
      $('#caShip').replaceWith('<div>Test</div>'); 
     } 
    }); 
}); 
</script> 

편집 :은 당신의 코멘트를 해결하기 위해 jQuery를 업데이트했습니다. 이것은 나의 기본적인 테스트에서 작동합니다.

+0

행운입니다. 페이지는 다시로드되지만 변경되지 않습니다. – MJ3111

+0

@ MJ3111 필자는 필자가 해준 작은 테스트에서 저에게 도움이되는 몇 가지 새로운 jQuery로 답변을 업데이트했습니다. –

+0

2 질문 ... 하나 : 큰 따옴표가 의도적입니까? .indexOf ("#"). 그리고 2는 첫번째 닫는 괄호에 ";"가 필요합니까? 페이지를 다시로드하지 않고이 작업을 수행하는 방법을 알아야한다고 생각합니다. 어떤 제안? 나는 행운으로 업데이트를 시도했다 – MJ3111

0

당신은 jQuery를 오용하고 실제로 함수를 선언하지 않습니다.

쓰기 $(function someName() { ... })은 명명 된 함수 식을 만들어 $ 함수에 전달합니다. 재사용 가능한 함수를 선언하지 않습니다. someName()을 쓸 수 없습니다.

또한 onclick 처리기의 통화가 완전히 잘못되었습니다.
함수를 호출하려면 호출해야합니다 (onclick="caShip();"); getElementById과 결합 할 수 없습니다.

$(function() { 
    $('#caShip').click(function() { 
     $(this).replaceWith('Some HTML (the new HTML has an id of replaced)'); 
    }); 
}); 
+0

나는 이것을 시도하고 당신의 제안의 HTML 부분이 Michael I 또는 Dutchie와 유사 할 것이라고 추측 할 수 있습니까? 그 말이 맞다면 도움이 안된다. HTML이 대체되지 않습니다. 페이지가 다시로드되는 것 같습니다 (href 때문에 가정합니다). 어쩌면이 방법으로 페이지를 다시로드 할 때 함수를 호출해야합니다. 그게 가능하니? 또한 페이지에 다른 기본 URL이 있기 때문에 '#reloaded'를 href로 사용할 수 없습니다. – MJ3111

+0

도움에 감사드립니다. 원래 질문을 보면 작업 코드로 업데이트했습니다. – MJ3111

관련 문제