将onclick动作添加到mathml

时间:2018-04-07 16:12:25

标签: javascript html mathml

我试图在点击下一页中的箭头时添加一些要执行的操作:



<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1">


	<script type="text/javascript">

		function createActions() {
			var div = document.getElementById("l");
			div.onclick=function(){alert("HI")};
		}

		
		window.onload = createActions;
	</script>
</head>
<body>

	<math>
		<mn>12</mn>
		<mo>&#x00D7;<!-- × --></mo>
		<mi>a</mi>
		<munderover>
			<mo>=</mo>
			<mo id="l" style="color: red;">&rarr;</mo>
			<mo style="color: red;">&larr;</mo>
		</munderover>
		<mi>24</mi>
	</math>
			
</body>
</html>
&#13;
&#13;
&#13;

调试器显示元素&#34; l&#34;通过调用&#34; getElementById&#34;返回,但&#34; onclick&#34;的定义。属性什么都不做。

除了之前的测试,我还测试了没有javascript和mathml:

<mo onclick='alert("HI")'>&rarr;</mo>

也没有成功。

1 个答案:

答案 0 :(得分:2)

使用addEventListener() Method

count = 1;
document.getElementById("l").addEventListener("click", function(){
    alert("You clicked "+ count++ +" times");
});
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

	<math>
		<mn>12</mn>
		<mo>&#x00D7;<!-- × --></mo>
		<mi>a</mi>
		<munderover>
			<mo>=</mo>
			<mo id="l" style="color: red;">&rarr;</mo>
			<mo style="color: red;">&larr;</mo>
		</munderover>
		<mi>24</mi>
	</math>
			
</body>
</html>