Making an svg image object clickable with onclick, avoiding absolute positioning

前端 未结 13 1065
轻奢々
轻奢々 2020-12-02 13:54

I have tried to change the images on my site from img to svg, changing img tags to embed and object tags. Bu

相关标签:
13条回答
  • 2020-12-02 14:30

    I wrapped the 'svg' tag in 'a' tag and put the onClick event in the 'a' tag

    0 讨论(0)
  • 2020-12-02 14:31

    Click on SVG's <g> element in <object> with click event. Works 100%. Take a look on the nested javascript in <svg>. Don't forget to insert window.parent.location.href= if you want to redirect the parent page.

    https://www.tutorialspoint.com/svg/svg_interactivity.htm

    0 讨论(0)
  • 2020-12-02 14:33

    It worked by simply replacing the <embed/> tag with <img/> and deleting the type attribute.

    For instance, in my code, instead of:

    <embed src=\"./images/info_09c.svg\" type=\"image/svg+xml\" width=\"45\" onClick='afiseaza_indicatie($i, \"$indicatii[$i]\")'> 
    

    which does not answer the clicking, I wrote:

    <img src=\"./images/info_09c.svg\" height=\"25\" width=\"25\" onClick='afiseaza_indicatie($i, \"$indicatii[$i]\")'> 
    

    It works in Internet Explorer and Google Chrome, and I hope that in the other browsers too.

    0 讨论(0)
  • 2020-12-02 14:35

    You could use following code:

    <style>
        .svgwrapper {
            position: relative;
        }
        .svgwrapper {
            position: absolute;
            z-index: -1;
        }
    </style>
    
    <div class="svgwrapper" onClick="function();">
        <object src="blah" />
    </div>
    

    b3ng0 wrote similar code but it does not work. z-index of parent must be auto.

    0 讨论(0)
  • 2020-12-02 14:35

    Perhaps what you're looking for is the SVG element's pointer-events property, which you can read about at the SVG w3C working group docs.

    You can use CSS to set what happens to the SVG element when it is clicked, etc.

    0 讨论(0)
  • 2020-12-02 14:39

    In case you're fine with wrapping the svg in another element (a for example) and putting onclick on the wrapper, svg {pointer-events: none;} CSS will do the trick.

    0 讨论(0)
提交回复
热议问题