在鼠标悬停javascript上加载js文件
大家好,有可能在鼠标悬停或单击等事件上加载js文件。在鼠标悬停javascript上加载js文件,javascript,javascript-events,Javascript,Javascript Events,大家好,有可能在鼠标悬停或单击等事件上加载js文件。 我尝试加载整个js文件而不是特定的函数。提前感谢。使用jQuery的示例 <!DOCTYPE html> <html> <head> <title>Test</title> <script type="text/javascript" src="http://code.jquery.com/jquery-
我尝试加载整个js文件而不是特定的函数。提前感谢。使用jQuery的示例
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#myButton').mouseenter(function() {
$("head").append($("<script />").prop("type", "text/javascript").prop("src", "http://code.jquery.com/jquery.mobile-1.2.0.js"));
});
});
</script>
</head>
<body>
<input type="button" id="myButton" value="CLICK" />
</body>
</html>
试验
$(文档).ready(函数(){
$(“#myButton”).mouseenter(函数(){
$(“head”).append($(“”).prop(“type”、“text/javascript”).prop(“src”),”http://code.jquery.com/jquery.mobile-1.2.0.js"));
});
});
此示例在按钮的onClick()
事件上加载指定的js文件
<button onclick="myFunction()">Click me</button>
<script type="text/javascript">
function myFunction(){
var file = document.createElement("script");
file.setAttribute("type", "text/javascript");
file.setAttribute("src", "js/js_file.js");
document.getElementsByTagName("head")[0].appendChild(file);
}
</script>
点击我
函数myFunction(){
var file=document.createElement(“脚本”);
setAttribute(“type”、“text/javascript”);
setAttribute(“src”、“js/js_file.js”);
document.getElementsByTagName(“head”)[0].appendChild(文件);
}
同样,您也可以在按钮或任何其他HTML元素的
onMouseOver()事件上加载js。抱歉,仅javascript无jquery