【问题标题】:Prevent browser from reloading main page after mouse event防止浏览器在鼠标事件后重新加载主页
【发布时间】:2015-12-01 05:04:03
【问题描述】:

我有一个网页,允许我将串行命令发送到我的树莓派上的串行端口。主页在 html 中,其中有图像,当我单击图像时,javascript 调用 cgi 文件,将命令发送到串行。在桌面浏览器上一切正常,当我按下图像时,我停留在同一页面上,但是当我在 iphone 或 ipad 上执行此操作时,它会将我重定向到 cgi 文件的位置。 这是我的代码 1。 HTML

<img src="img/sound.png" alt="sound" id="sound_b" ontouchstart="changeImageSound(), sound()" ontouchend="changeImageSound()">

2.JavaScript

function sound() {
document.location="helpers/sound.cgi";
}

3.cgi

#!/usr/bin/python
print "Status: 204 No Content"
print "Content-type: text/plain"
print ""
import cgi
import serial
import time

# Open a serial connection to Roomba
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200)

# Assuming the robot is awake, start safe mode so we can hack.
ser.write('\x83')
time.sleep(.1)

# Program a five-note start song into Roomba.
ser.write('\x8c\x00\x05C\x10H\x18J\x08L\x10O\x20')

# Play the song we just programmed.
ser.write('\x8d\x00')
time.sleep(1.6) # wait for the song to complete

# Leave the Roomba in passive mode; this allows it to keep
#  running Roomba behaviors while we wait for more commands.
ser.write('\x80')

# Close the serial port; we're done for now.
ser.close()

【问题讨论】:

    标签: python html web cgi


    【解决方案1】:

    您只需将其添加到您的 Javascript 代码中,

    event.preventDefault();
    

    【讨论】:

    • 到javascript函数?
    • 你需要把这个放到javascript函数中
    【解决方案2】:

    您可以将下面提到的 javascript 代码添加到声音函数中,但它需要其中的事件对象。否则你可以直接在 ontouchstart 中使用它

    event.preventDefault();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-11
      • 2012-05-21
      • 2012-03-28
      • 2022-01-18
      • 2016-11-15
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      相关资源
      最近更新 更多