문제

I have the below HTML code

<a href="http://www.google.com">Google</a>

When I click on the link, I want my string to contains the full HTML code below

<a href="http://www.google.com">Google</a>

I tried using this method, but I only manage to get the text "Google", can't get the anchor code.

$('#frame').contents().find('body').on("click", "a",  function () {
    var string = $(this).html();
    alert(string);
});

String alert only Google when I need <a href="http://www.google.com">Google</a>.

도움이 되었습니까?

해결책

Use this.outerHTML, or wrap the element in a container, and use .html() on that container.

$('#frame').contents().find('body').on("click", "a",  function () {
    var string = this.outerHTML;
    alert(string);
});

I prefer outerHTML, but Firefox has only recently added support for it. So, to account for these browsers, the second method might be preferred.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top