HTML中的JavaScript和SVG
我有JavaScript文件。 我有SVG文件。 我也有HTML文件HTML中的JavaScript和SVG,javascript,html,svg,Javascript,Html,Svg,我有JavaScript文件。 我有SVG文件。 我也有HTML文件 <head> ... <link rel="stylesheet" href="style.css" type="text/css" charset="utf-8"> ... </head> <body> ... <img src='svgfile.svg' type='image/svg+xml' /> ... </body> ... ... ...
<head>
...
<link rel="stylesheet" href="style.css" type="text/css" charset="utf-8">
...
</head>
<body>
...
<img src='svgfile.svg' type='image/svg+xml' />
...
</body>
...
...
...
...
有人知道我如何在JavaScript中调用SVG元素吗?(以圆圈为例)如果要使用Javascript与加载到HTML5页面的svg文件进行动态交互,则必须以内联方式加载svg。如果作为
加载,则无法使用javascript对其进行编程。但是,您可以通过XMLHttpRequest
将svg文件作为xml加载,并用响应填充DIV的innerHTML
。然后可以通过Javascript动态更改此内联SVG。这适用于所有浏览器。尝试下面的文件
假设您有一个SVG文件(my.SVG)
您的HTML5文件如下所示:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<center>
<div id="svgInlineDiv" style='background-color:lightgreen;width:400px;height:400px;'></div>
<button onClick=changeInlineCircleColor()>Change Circle Color</button>
<div id="svgObjectDiv" style='background-color:lightblue;width:400px;height:400px;'>
<object data="my.svg" type="image/svg+xml" id="objsvg" width="100%" height="100%"></object>
</div>
</center>
<script id=myScript>
function changeInlineCircleColor()
{
myCircle.setAttribute("fill","red")
}
</script>
<script>
document.addEventListener("onload",inlineSVG(),false)
function inlineSVG()
{
var SVGFile="my.svg"
var loadXML = new XMLHttpRequest;
function handler(){
if(loadXML.readyState == 4 && loadXML.status == 200)
svgInlineDiv.innerHTML=loadXML.responseText
}
if (loadXML != null){
loadXML.open("GET", SVGFile, true);
loadXML.onreadystatechange = handler;
loadXML.send();
}
}
</script>
</body>
</html>
改变圆圈颜色
函数更改LineCircleColor()
{
myCircle.setAttribute(“填充”、“红色”)
}
document.addEventListener(“onload”,inlineSVG(),false)
函数inlineSVG()
{
var SVGFile=“my.svg”
var loadXML=newXMLHttpRequest;
函数处理程序(){
if(loadXML.readyState==4&&loadXML.status==200)
svgInlineDiv.innerHTML=loadXML.responseText
}
if(loadXML!=null){
loadXML.open(“GET”,SVGFile,true);
loadXML.onreadystatechange=handler;
loadXML.send();
}
}
也许您可以使用这个答案:请注意,您无法访问
元素的内容,如果您想这样做,您需要使用
或
标记。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<center>
<div id="svgInlineDiv" style='background-color:lightgreen;width:400px;height:400px;'></div>
<button onClick=changeInlineCircleColor()>Change Circle Color</button>
<div id="svgObjectDiv" style='background-color:lightblue;width:400px;height:400px;'>
<object data="my.svg" type="image/svg+xml" id="objsvg" width="100%" height="100%"></object>
</div>
</center>
<script id=myScript>
function changeInlineCircleColor()
{
myCircle.setAttribute("fill","red")
}
</script>
<script>
document.addEventListener("onload",inlineSVG(),false)
function inlineSVG()
{
var SVGFile="my.svg"
var loadXML = new XMLHttpRequest;
function handler(){
if(loadXML.readyState == 4 && loadXML.status == 200)
svgInlineDiv.innerHTML=loadXML.responseText
}
if (loadXML != null){
loadXML.open("GET", SVGFile, true);
loadXML.onreadystatechange = handler;
loadXML.send();
}
}
</script>
</body>
</html>