<object> 요소가 참조하는 svg에서 부모 문서에 정의 된 함수를 호출하는 방법은 무엇입니까?

YY1988

내 웹 페이지에 svg 파일을 삽입했습니다.

    <object id="svgmap" width="100%" height="100%" data="<%=path %>/svg/map.svg" type="image/svg+xml" style="">
    </object>

그런 다음 "support.js"라는 외부 자바 스크립트 파일을 사용하여이 svg 파일에 이미지 태그를 추가했습니다.

    var marker = document.createElementNS(xmlns, 'image');
    marker.setAttributeNS(null, 'id', "img_0123");
    marker.setAttributeNS('http://www.w3.org/1999/xlink', 'href', iconurl);
    marker.setAttributeNS(null, 'x', posX);
    marker.setAttributeNS(null, 'y', posY);
    marker.setAttributeNS(null, 'visibility', 'visible');
    marker.setAttribute("onclick", "onsel('0123')");

이 "onsel"함수는 support.js에도 정의되어 있으므로 이미지를 클릭하면 다른 이미지로 변경됩니다. 이제 브라우저 (Opera, Mozilla 및 Chrome)에서이 페이지를 볼 때 이미지를 클릭했을 때 다른 이미지로 바뀌지 않았고 개발자 도구의 콘솔에서 "referenceerror : onsel is not defined svgmap : 1 ".

이는 svg 파일이 다른 자바 스크립트 파일에 정의 된 기능을 사용할 수 없음을 의미합니까? 그렇게하는 방법?

에릭 달 스트롬

I've interpreted your question as "how do I call functions defined in the scope of a parent html document from an svg referenced by an <object> element?". It's a little unclear from the question where your "onsel" function is defined, but I'm guessing it's in a script inside the html document.

Here's an example of how to call from inside the svg out to the parent. The function in the parent prints what element was clicked and which element it was referenced from.

From svg to parent document:

    window.parent.yourFunctionInParentDocument();

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관