development

HTML 태그

big-blog 2020. 7. 29. 07:24
반응형

HTML 태그 는 href 및 onclick 작동을 모두 추가하려고합니다.


HTML 태그에 대해 문의하고 싶습니다

<a href="www.mysite.com" onClick="javascript.function();">Item</a>

hrefonClick 과 함께 작동 하는 태그 를 만드는 방법은 무엇입니까? (onClick을 먼저 실행하고 href를 선호)


약간의 구문 변경으로 이미 필요한 것을 가지고 있습니다.

<a href="www.mysite.com" onclick="return theFunction();">Item</a>

<script type="text/javascript">
    function theFunction () {
        // return true or false, depending on whether you want to allow the `href` property to follow through or not
    }
</script>

의 기본 동작 <a>태그의 onclickhref속성을 실행하는 것입니다 onclick, 다음을 따라 href만큼이 같은 onclick반환하지 않는 false이벤트를 취소 (또는 이벤트가 방지되지 않은)


jQuery를 사용하십시오 . click이벤트 를 캡처 한 다음 웹 사이트로 이동해야합니다.

$("#myHref").on('click', function() {
  alert("inside onclick");
  window.location = "http://www.google.com";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="myHref">Click me</a>


이 HTML을 사용하려면 다음을 수행하십시오.

<a href="www.mysite.com" onclick="make(event)">Item</a>

<script>
    function make(e) {
        // ...  your function code
        // e.preventDefault();   // use this to NOT go to href site
    }
</script>

다음은 실제 예제 입니다.


ng-click대신에 사용하십시오 onclick. 그리고 그처럼 간단합니다.

<a href="www.mysite.com" ng-click="return theFunction();">Item</a>

<script type="text/javascript">
function theFunction () {
    // return true or false, depending on whether you want to allow 
    // the`href` property to follow through or not
 }
</script>

참고 URL : https://stackoverflow.com/questions/14867558/html-tag-a-want-to-add-both-href-and-onclick-working

반응형