Javascript 阻止我的HTML按钮离开页面
这可能真的很简单,但谷歌搜索并没有帮到我。所以我有一个机器人连接到网络上,它对URL上的查询做出响应 我只想做几个简单的按钮,发送这些查询,但不会离开页面。目前,我有下面的HTML,但每次我按下一个按钮,它就会转到有问题的URL并离开主页Javascript 阻止我的HTML按钮离开页面,javascript,jquery,html,Javascript,Jquery,Html,这可能真的很简单,但谷歌搜索并没有帮到我。所以我有一个机器人连接到网络上,它对URL上的查询做出响应 我只想做几个简单的按钮,发送这些查询,但不会离开页面。目前,我有下面的HTML,但每次我按下一个按钮,它就会转到有问题的URL并离开主页 <!DOCTYPE html> <html> <body> <strong>MyRIO Robot Controller</strong> <input type=
<!DOCTYPE html>
<html>
<body>
<strong>MyRIO Robot Controller</strong>
<input type=button onClick="parent.location='http://172.16.0.1:8001/web/connect?Connect=1'" value='Start Control'>
<input type=button onClick="parent.location='http://172.16.0.1:8001/web/connect?Connect=0'" value='Stop Control'>
<input type=button onClick="parent.location='http://172.16.0.1:8001/web/Speed?Speed1=100'" value='Full Speed'>
<input type=button onClick="parent.location='http://172.16.0.1:8001/web/Speed?Speed1=50'" value='Half Speed'>
<input type=button onClick="parent.location='http://172.16.0.1:8001/web/Speed?Speed1=00'" value='Stop'>
</body>
</html>
MyRIO机器人控制器
所以AJAX似乎并不难学。对于其他有这个问题的人,我提出了一个解决方案
<!DOCTYPE html>
<html>
<head>
<script>
function startControl()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://172.16.0.1:8001/web/connect?Connect=1",true);
xmlhttp.send();
}
function stopControl()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://172.16.0.1:8001/web/connect?Connect=0",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>MyRIO Robot Controller</h2>
<button type="button" onclick="startControl()">Start Control</button>
<button type="button" onclick="stopControl()">Stop Control</button>
<div id="myDiv"></div>
函数startControl()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“myDiv”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”http://172.16.0.1:8001/web/connect?Connect=1“,对);
xmlhttp.send();
}
函数stopControl()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“myDiv”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”http://172.16.0.1:8001/web/connect?Connect=0“,对);
xmlhttp.send();
}
MyRIO机器人控制器
启动控制
停止控制
如果你不想在点击按钮时发布或获取信息,你必须使用AJAX。AJAX是你的解决方案,只要它在同一个域上,其他包括作为iframe的页面也是可能的。好吧,我现在有点进展了,你有我可以使用的AJAX示例吗(我甚至不知道AJAX是什么,但我愿意学习)有很多关于ajax的教程。。。。让谷歌成为你的朋友