⑴ js中监听iframe点击事件
在js中,需要监听iframe的点击事件,但是因为浏览器的同源策略,是无法监听到的,只能另辟蹊径去解决它
注意,仅限于pc网站,意思就是必须要有鼠标移入移出iframe的操作
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Detect IFrame Clicks</title>
<script src="./jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var currentObj=Object
var isOverIFrame = false;
function processMouseOut(e) {
console.log('out iframe',e.target);
currentObj=e.target
isOverIFrame = false;
top.focus();
}
function processMouseOver(e) {
// log("IFrame mouse >> OVER << detected.");
console.log('in iframe',e.target);
currentObj=e.target
isOverIFrame = true;
}
function processIFrameClick(event) {
if(isOverIFrame) {
console.log('click iframe',event.target,currentObj);
}
}
function attachOnloadEvent(func, obj) {
if(typeof window.addEventListener != 'undefined') {
window.addEventListener('load', func, false);
} else if (typeof document.addEventListener != 'undefined') {
document.addEventListener('load', func, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onload', func);
} else {
if (typeof window.onload == 'function') {
var oldonload = onload;
window.onload = function() {
oldonload();
func();
};
} else {
window.onload = func;
}
}
}
function init() {
var element = document.getElementsByTagName("iframe");
for (var i=0; i<element.length; i++) {
element[i].onmouseover = processMouseOver;
element[i].onmouseout = processMouseOut;
}
if (typeof window.attachEvent != 'undefined') {
top.attachEvent('onblur', processIFrameClick);
}
else if (typeof window.addEventListener != 'undefined') {
top.addEventListener('blur', processIFrameClick, false);
}
}
attachOnloadEvent(init);
});
</script>
</head>
<body>
<iframe src="https://www.hao123.com" width="80%" height="600px"></iframe>
<iframe src="https://www..com" width="80%" height="600px"></iframe>
</form>
</body>
</html>
复制上边整段html,打开console控制台,即可解决这个问题,亲测好用
⑵ 关于js中事件监听的问题(文本框 回车键 失去焦点)
你可以在输入框里面一个js事件onkeydown=“document.getElementById('确定按钮的Id').focus()”
如果还不行把你的代码贴出来我看下
⑶ js怎么监听鼠标是否有操作
<script type="text/javascript">
// 移动了就更新最近一次移动的时间。
document.onmousemove = function(){
window.lastMove = new Date().getTime();
};
window.lastMove = new Date().getTime();//最近一次移动时间
window.setInterval(function(){//每1秒钟检查一次。
var now = new Date().getTime();
// 如果超时了
if( now - lastMove > 指定时间 ){
// 自己写了撒。
}
}, 1000);
</script>
⑷ JAVA 求助 就是如何用鼠标监听事件调用Graphics 画的线条和矩形 接着画 不会画完
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Test{
public static void main(String[] args){
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFrame extends JFrame implements ActionListener{
MyPanel panel = new MyPanel();
JToolBar t;
JButton obj1;
JButton obj2;
JButton obj3;
public MyFrame(){
obj1 = new JButton("Line");
obj2 = new JButton("yuan");
obj3 = new JButton("juxing");
t = new JToolBar();
obj1.addActionListener(this);
obj2.addActionListener(this);
obj3.addActionListener(this);
t = new JToolBar();
t.add(obj1);
t.add(obj2);
t.add(obj3);
setTitle("DrawTest");
setSize(W,H);
Container contentPane = getContentPane();
contentPane.add(t,BorderLayout.NORTH);
contentPane.add(panel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==obj1)
panel.shape=0;//直线
if(e.getSource()==obj2)
panel.shape=1;//円
if(e.getSource()==obj3)
panel.shape=2;//长方形
}
public static final int W = 400;
public static final int H = 400;
}
class MyPanel extends JPanel implements MouseListener{
private ArrayList<Point> pointList = new ArrayList<Point>();
int shape = -1;
int x1 = 0,y1 = 0;
int x2 = 0,y2 = 0;
MyPanel(){
addMouseListener(this);
}
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
pointList.add(new Point(x1,y1));
}
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
pointList.add(new Point(x2,y2));
repaint();
}
public void mouseClicked(MouseEvent e){
}
public void mouseMoved(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for(int i =0 ;i<pointList.size()-1;i=i+2){
Point p1 = pointList.get(i);
Point p2 = pointList.get(i+1);
switch(shape){
case 0:
g.drawLine(p1.x,p1.y,p2.x,p2.y);
break;
case 1:
int width1 = p2.x - p1.x;
int height1 = p2.y - p1.y;
g.drawOval(p1.x,p1.y,width1,height1);
break;
case 2:
int width2 = p2.x - p1.x;
int height2 = p2.y - p1.y;
g.drawRect(p1.x,p1.y,width2,height2);
break;
default:
System.out.println("please once again!");
break;
}
}
}
}
⑸ JS鼠标跟随用什么来解决啊
你说的什么,我没看明白。
JS中 window.event 对象可以监听键盘和鼠标的一些动作。可以获取鼠标当前的位置。设置其他对象的坐标就可以实现跟随
如:鼠标X坐标为 window.event.clientX
属性 描述
Abstract 使用 event 对象获取高级流重定向器(ASX)文件中项目横幅的 Abstract 内容。
altKey 设置或获取 Alt 键的状态。
altLeft 设置或获取左 Alt 键的状态。
Banner 使用 event 对象获取高级流重定向器(ASX)文件中项目的 Banner 内容。
button 设置或获取用户所按的鼠标按钮。
cancelBubble 设置或获取当前事件是否要在事件句柄中向上冒泡。
clientX 设置或获取鼠标指针位置相对于窗口客户区域的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条。
clientY 设置或获取鼠标指针位置相对于窗口客户区域的 y 坐标,其中客户区域不包括窗口自身的控件和滚动条。
contentOverflow 获取表明文档处理当前 LayoutRect 对象后是否包含附加的内容。
ctrlKey 设置或获取 Ctrl 键的状态。
ctrlLeft 设置或获取左 Ctrl 键的状态。
dataFld 设置或获取 oncellchange 事件影响的数据列。
fromElement 设置或获取事件发生时激活或鼠标将要离开的对象。
keyCode 设置或获取与导致事件的按键关联的 Unicode 按键代码。
MoreInfo 通过 event 对象获取高级流重定向器(ASX)文件中项目横幅的 MoreInfo 内容。
nextPage 获取打印模板中下页的位置。
offsetX 设置或获取鼠标指针位置相对于触发事件的对象的 x 坐标。
offsetY 设置或获取鼠标指针位置相对于触发事件的对象的 y 坐标。
propertyName 设置或获取对象上发生更改的属性名称。
qualifier 设置或获取由数据源对象提供的数据成员的名称。
reason 设置或获取数据源对象的数据传输结果。
recordset 从数据源对象设置或获取对默认数据集的引用。
repeat 获取 onkeydown 事件是否正在重复。
returnValue 设置或获取事件的返回值。
saveType 当 oncontentsave 触发时获取剪贴板类型。
screenX 设置或获取获取鼠标指针位置相对于用户屏幕的 x 坐标。
screenY 设置或获取鼠标指针位置相对于用户屏幕的 y 坐标。
shiftKey 设置或获取 Shift 键的状态。
shiftLeft 设置或获取左 Shift 键的状态。
srcElement 设置或获取触发事件的对象。
srcFilter 设置或获取触发 onfilterchange 事件的滤镜对象。
srcUrn 获取触发事件的行为的统一资源名称(URN)。
toElement 设置或获取用户要将鼠标指针移动指向的对象的引用。
type 从 event 对象中获取事件名称。
wheelDelta 设置或获取滚轮按钮滚动的距离和方向。
x 设置或获取鼠标指针位置相对于父文档的 x 像素坐标。
y 设置或获取鼠标指针位置相对于父文档的 y 像素坐标。
希望对你有帮助
⑹ java监听鼠标事件的问题
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test extends JFrame implements Runnable{
public static int x,y,direction=0;
public test(){
this.setSize(600,400);
this.setVisible(true);
x=this.getContentPane().getWidth()/2;
y=this.getContentPane().getHeight()/2;
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN:
direction=1;break;
case KeyEvent.VK_LEFT:
direction=2;break;
case KeyEvent.VK_RIGHT:
direction=3;break;
case KeyEvent.VK_UP:
direction=4;break;
}
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
test t=new test();
new Thread(t).start();
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.red);
g.drawRect(x-60,y-40,120,80);
}
public void run(){
while(true){
try{
Thread.sleep(100);
}catch(Exception e){}
switch(direction){
case 1: y =4;break;
case 2: x-=4;break;
case 3: x =4;break;
case 4: y-=4;break;
}
this.repaint();
}
}
}
⑺ 什么用javascript让一个对象同时监听鼠标单击和鼠标双击事件
<input name="test" id="test" type="button" onmouseup="c()" value="单双击测试"/>
注意不是onclick,是内onmouseup
var count = 0;
var timer ;
function c(){
count ++;
timer = window.setTimeout(function(){
if(count==1) alert("单击容");
else alert("双击");
window.clearTimeout(timer);
count=0;
},300)
}