下面的代碼是一個簡單的表單,它接受來自用戶的數字並顯示在表單中輸入的半徑的圓。一切工作都很好,只是當我點擊提交按鈕時,圓圈就消失了。如何解決這個問題?我點擊提交按鈕後,我希望該圈子在那裏很久。使用SVG在html頁面中顯示動態圓圈
<title>Dynamic Vector Circle</title>
<script type="text/javascript">
var svgNS = "http://www.w3.org/2000/svg";
function createCircle()
{
var myCircle = document.createElementNS(svgNS,"circle"); //to create a circle, for rectangle use rectangle
var radius= document.getElementById("rad").value;
myCircle.setAttributeNS(null,"id","mycircle");
myCircle.setAttributeNS(null,"cx",100);
myCircle.setAttributeNS(null,"cy",100);
myCircle.setAttributeNS(null,"r",radius);
myCircle.setAttributeNS(null,"fill","black");
myCircle.setAttributeNS(null,"stroke","none");
document.getElementById("mySVG").appendChild(myCircle).innerHTML;
}
</script>
<form>
<input type="text" id="rad" placeholder="Enter radius">
<button onClick="createCircle();">Submit</button>
<svg id="mySVG" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
</svg>
</form>
我沒有看到JSFiddle的這種行爲:http://jsfiddle.net/252yLdqx/ – Cymen
@Cymen,你的代碼不在'form'裏面,就像在OP – Grundy
@Grundy好點! – Cymen