HTML:svg对象和脚本应该改变svg对象的颜色但是由于某种原因它没有?
<object id="object" type="image/svg+xml" data="play-pattern--light.svg"></object>
<script type="text/javascript">
window.onload=function () {
var a = document.getElementById("object");
var svgDoc = a.contentDocument;
var styleElement = svgDoc.createElementNS("http://www.w3.org/2000/svg", "style");
styleElement.textContent = ".st0 { fill: #000 }";
svgDoc.getElementById("object").appendChild(styleElement);
};
</script>
我认为这就是为SVG对象着色所需的全部内容
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="160px" height="160px" viewBox="0 0 160 160" style="enable-background:new 0 0 160 160;" xml:space="preserve">
<style type="text/css">
.st0{fill:#EDF5F5;}
</style>
需要一些指导,为什么我的代码不起作用,它也说错误&#34; Uncaught TypeError:无法读取属性&quot; appendChild&#39; of null&#34;
由于
答案 0 :(得分:1)
您应该使用:
public class SimpleTest extends TimerTask {
JFrame frame;
int count ,x1,y1,x2,y2;
public void run() {
count+=5;
x1=count;
y1=count;
x2=count-1;
y2=count-1;
// repaint(); i want to invoke paintcomponent method from here , simply to change the end point of line every secd
}
void guiMethod(){
frame=new JFrame("Libra's");
frame.setBounds(50, 50, 250, 250);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new NewPanel());
}
public static void main(String[] args){
SimpleTest c=new SimpleTest();
c.guiMethod();
Timer timer = new Timer();
timer.schedule(new SimpleTest(), 1000,1000);
}
}
class NewPanel extends JPanel{
SimpleTest obj=new SimpleTest();
@Override
protected void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.setStroke(new BasicStroke(3));
g.drawLine(120, 120, 70, 80);
g.drawLine(120, 120, obj.x1, obj.y1);
g.drawLine(120, 120, obj.x2, obj.y2);
}}
而不是
svgDoc.documentElement.appendChild(styleElement);