代码之家  ›  专栏  ›  技术社区  ›  John Duff

如何将SVG和HTML混合到一个页面中?

  •  6
  • John Duff  · 技术社区  · 16 年前

    jQuery.svg plugin

    <!DOCTYPE HTML>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg" >
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        </head>
    
        <body>
    
        <div class="map editable" id="map_1"><svg height="600" version="1.1" width="600" xmlns="http://www.w3.org/2000/svg"><image height="600" href="/system/graphics/1/original/floor-plan-large.gif" width="500" x="0" y="0" /><circle cx="300" cy="580" fill="red" r="5" stroke-width="2" stroke="red" /><circle cx="300" cy="400" fill="red" r="5" stroke-width="2" stroke="red" /><circle cx="260" cy="400" fill="red" r="5" stroke-width="2" stroke="red" /><circle cx="260" cy="340" fill="red" r="5" stroke-width="2" stroke="red" /><circle cx="140" cy="340" fill="red" r="5" stroke-width="2" stroke="red" /><polyline fill="none" points="300,580 300,400 260,400 260,340 140,340" stroke-width="3" stroke="blue" /></svg></div>
    
        <svg version="1.1" baseProfile="full" width="300px" height="200px" xmlns="http://www.w3.org/2000/svg">
          <circle cx="150px" cy="100px" r="50px" fill="#ff0000" stroke="#000000" stroke-width="5px"/>
        </svg>
    
    
        <svg:svg version="1.1" baseProfile="full" width="300px" height="200px">
          <svg:circle cx="150px" cy="100px" r="50px" fill="#ff0000" stroke="#000000" stroke-width="5px"/>
        </svg:svg>
    
        </body>
    </html>
    

    我是否需要一个meta标记来表示页面中有SVG内容,或者以某种方式定义SVG名称空间?

    3 回复  |  直到 16 年前
        1
  •  1
  •   ChristopheD    16 年前

    看见 this link

    内联SVG示例 can be seen here .

        2
  •  1
  •   schlebe    6 年前

    SVG 现在所有浏览器都可以使用html或xhtml格式。

    我使用的HTML/SVG(2.0)代码示例

    <?xml version="1.0" encoding="UTF-8" ?>
    <html xmlns:svg="http://www.w3.org/2000/svg">
    <head> <title>SVG within XHTML Demo</title> </head>
    <body>
      <p>
        <b>Electricity Plan</b>
      </p>
      <svg xmlns='http://www.w3.org/2000/svg' version='2.0' width='8000px' height='6000px'>
        <defs>
          <g id="prise">
            <line x1="0" y1="0" x2="20" y2="0" stroke="blue"/>
            <line x1="20" y1="-15" x2="20" y2="15" stroke="blue"/>
            <path d="m40,-20 a20,20 0 0,0 0,40" fill="none" stroke="blue"/>
            <line x1="40" y1="-20" x2="40" y2="-28" stroke="blue"/>
            <line x1="40" y1="20" x2="40" y2="28" stroke="blue"/>
          </g>
        </defs>
        <line x1='100' y1='50' x2='140' y2='50' stroke='blue'/>
        <use href='#prise' x='140' y='50'/>
      </svg>
    </body>
    </html>
    
        3
  •  0
  •   KZ.    16 年前

    你的文档类型是什么?确保它与示例相同,即XHTML,而不是html4.01。

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html 
              PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    推荐文章