简体   繁体   中英

How can I pass values from a function to SVG tag?

I want to make the header of my application dynamically and modular, so I'm writing a function in Javascript that will color the header (SVG) dynamically, but how can I include SVG into my function (such as: document.getElementByTagName(SVG) or something?).

This is for an webapp, I want to make a function headerStyle(title, pageSVG1, pageSVG2, pageSVG3) , which will style the header, with the correct SVG formats and colors which are passed into the function. For example: headerstyle('Menu', 'FF0000', '000000', 'D3D3D3') . I'm not sure how to begin to set up this function.

<svg width="100%" height="262" viewBox="0 0 375 262" fill="none" xmlns="http://www.w3.org/2000/svg">
    <g>
        <circle cx="188" cy="-803" r="1065" fill="FF0000"/>
        <path d="M1046.5 -839.5C1046.5 -251.317 569.683 225.5 -18.5 225.5C-606.683 225.5 -1083.5 -251.317 -1083.5 -839.5C-1083.5 -1427.68 -606.683 -1904.5 -18.5 -1904.5C569.683 -1904.5 1046.5 -1427.68 1046.5 -839.5Z" fill="000000"/>
        <path d="M652 -635.5C652 -156.393 263.607 232 -215.5 232C-694.607 232 -1083 -156.393 -1083 -635.5C-1083 -1114.61 -694.607 -1503 -215.5 -1503C263.607 -1503 652 -1114.61 652 -635.5Z" fill="D3D3D3"/>
        <text id="page-title" style="fill: white; font-size: 28px; font-family: 'Lato', sans-serif; font-weight: 700; padding: 50px;" x="50" y="120">Menu</text>
    </g>
</svg>

Ìnto the function:

function headerStyle(title, pageSVG1, pageSVG2, pageSVG3) {
    document.getElementByTagName("SVG");
}

I honestly don't know how I'm gonna get it working. Could you please help or show me an example of how I can achieve this? If passed headerStyle("Menu", "FF00000", "000000", "D3D3D3") , I want to get the color of the first circle #FF00000 , the second #000000 , but I don't know how I can write a function like that. The output is not what it should be.

As I've told you in my comment you need to remove fill="none" . Then you can write a function to change the style of the SVG element like this.

 let svg = document.querySelector("svg"); function headerStyle(svg,color){svg.setAttribute("style",`fill:${color}`)} headerStyle(svg,"gold") 
 <svg width="100%" height="262" viewBox="0 0 375 262" fill="none" xmlns="http://www.w3.org/2000/svg"> <g> <circle cx="188" cy="-803" r="1065" fill="FF0000"/> <path d="M1046.5 -839.5C1046.5 -251.317 569.683 225.5 -18.5 225.5C-606.683 225.5 -1083.5 -251.317 -1083.5 -839.5C-1083.5 -1427.68 -606.683 -1904.5 -18.5 -1904.5C569.683 -1904.5 1046.5 -1427.68 1046.5 -839.5Z" fill="000000"/> <path d="M652 -635.5C652 -156.393 263.607 232 -215.5 232C-694.607 232 -1083 -156.393 -1083 -635.5C-1083 -1114.61 -694.607 -1503 -215.5 -1503C263.607 -1503 652 -1114.61 652 -635.5Z" fill="D3D3D3"/> <text id="page-title" style="fill: white; font-size: 28px; font-family: 'Lato', sans-serif; font-weight: 700; padding: 50px;" x="50" y="120">Menu</text> </g> </svg> 

UPDATE

The OP is commenting:

But what if I want to change the fill of the inside the tag or the fill of the tag inside the tag?

In this case you need to select the circle or/and the path and change the fill for those:

let circle = document.querySelector("circle");
circle.setAttribute("style",`fill:${color}`);

You can also change the function to something like:

function changeStyle(svgElmt,color){svgElmt.setAttribute("style",`fill:${color}`)}

Now you can use the function for the any svg element:

changeStyle(circle,"red")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM