반응형
HTML 태그 는 href 및 onclick 작동을 모두 추가하려고합니다.
HTML 태그에 대해 문의하고 싶습니다
<a href="www.mysite.com" onClick="javascript.function();">Item</a>
href 및 onClick 과 함께 작동 하는 태그 를 만드는 방법은 무엇입니까? (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>
태그의 onclick
와 href
속성을 실행하는 것입니다 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
반응형
'development' 카테고리의 다른 글
C ++ 11에서 "return {}"문장의 의미는 무엇입니까? (0) | 2020.07.29 |
---|---|
WPF ListView 선택 해제 (0) | 2020.07.29 |
다른 파일에서 변수를 가져 오시겠습니까? (0) | 2020.07.29 |
RGB 값이 주어지면 색조 (또는 음영)를 어떻게 만듭니 까? (0) | 2020.07.29 |
Bootstrap 4의 '.well'에 해당하는 클래스는 무엇입니까 (0) | 2020.07.29 |