首页:
...
<script>
if (window.addEventListener) {
// addEventListener equivalent of code below
} else if (window.attachEvent) {
window.attachEvent("message", function(e) {
if (e.origin != "page2.html") { return; }
alert(e.data);
});
}
</script>
<iframe src="page2.html"></iframe>
Run Code Online (Sandbox Code Playgroud)
page2.html:
<script>
var message = "hello!";
parent.postMessage(message, '*');
</script>
Run Code Online (Sandbox Code Playgroud)
此代码在Chrome,FF和Opera中运行良好.当然,IE有自己的做事方式,所以尽管使用了自己的代码,这些代码仍无法正常工作.attachEvent.
page2.html实际上是另一个域的页面; 我正在发送正确的P3P标头(应该没关系,但就是这样).
我如何找出为什么postMessage似乎没有到达父页面?
我无法让观察者真正在Ember.js做任何事情.基本上,我有一个Ember.Select下拉菜单,并希望将更多动作绑定到选择下拉列表中的值的事件.例如:
App.selectedPersonController = Ember.Object.create({
person: null,
personDidChange: function() {
// do something here when the person changes
console.log("PERSON CHANGED")
}.observes('person')
});
Run Code Online (Sandbox Code Playgroud)
即使'人'属性正在更新,也没有任何事情发生.有什么建议?
我遇到了一个非常奇怪的问题.我将touchstart事件绑定到一个元素,并希望检索事件的X和Y坐标.所有在线文档都表明可以通过以下方式之一获得:
e.touchese.targetTouchese.changedTouches但是,以下代码在从jQuery运行时调用所有三个假定数组的#test返回undefined:
$('#test').bind('touchstart', function(e) {
alert(e.targetTouches);
alert(e.touches);
alert(e.changedTouches);
});
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到一个演示> http://jsfiddle.net/c7UKV/1/(你需要在移动设备上查看它,或者你可以直接到这里的结果> http://jsfiddle.net/c7UKV/ 1/embedded/result /).
我的问题是有没有人知道一个解决方案,或者是jQuery以某种我不知道的方式规范事件?我测试的所有版本的jQuery(1.6+)以及iOS模拟器和物理iDevices都存在这个问题.
作为对上述内容的更新,使用vanilla JavaScript(而不是jQuery)调用它会返回正确的结果.例如,以下代码可以正常运行:
document.getElementById('test').addEventListener('touchstart', checkCoords, false);
function checkCoords(e)
{
alert(e.targetTouches);
alert(e.touches);
alert(e.changedTouches);
}
Run Code Online (Sandbox Code Playgroud)
所以看起来这是一个jQuery问题,而不是其他任何问题.您可以在此jsFiddle中看到上述正常工作.
我想绕过:
An invalid form control with name='dob_mm' is not focusable.
Run Code Online (Sandbox Code Playgroud)
Chrome中出现错误.是的,我们有一堆必需的字段,是的,这些字段是隐藏的.表单分为多个选项卡,一次只显示一个.
是否有某种类型的Jquery函数可以显示某些类型的弹出窗口"你必须填写其他选项卡中的信息"?
我看这里,但没有告诉我太多.
谢谢!
javascript error-handling jquery google-chrome javascript-events
我试图让这个代码工作,我不能为我的生活理解为什么它不会.我已经尝试过JSlint和一切.对不起新手问题,我用JavaScript花了不到2个小时,所以这是一个愚蠢的问题.谢谢.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="Style/demo.css" />
<script type="text/javascript">
window.onload = init;
function init() {
document.getElementById("s1").onclick = print(1);
document.getElementById("s2").onclick = print(2);
document.getElementById("s3").onclick = print(3);
document.getElementById("s4").onclick = print(0);
}
function print(printno) {
var demo = document.getElementById("demo");
switch(printno)
{
case 1:
demo.innerHTML= "It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire. ";
break;
case 2:
demo.innerHTML = "During the battle, Rebel spies …Run Code Online (Sandbox Code Playgroud) 我正在开发一个游戏,我想抽象我的ui,并根据各种游戏状态绑定非绑定事件.但我无法弄清楚为什么这个事件没有被删除.似乎处理程序中的范围是正确的.
相关(剥离)js:
var controls = {
game : {
el : null,
cb : null,
bind : function(el, cb) {
this.el = el;
this.cb = cb;
this.el.addEventListener('click', this.handler.bind(this), true);
},
unbind : function() {
console.log('unbind');
this.el.removeEventListener('click', this.handler, true);
},
handler : function() {
this.cb();
this.unbind();
}
}
};
var manager = {
init : function() {
var c = document.getElementById('c');
controls.game.bind(c, this.action.bind(this));
},
action : function() {
console.log('c clicked');
}
};
manager.init();
Run Code Online (Sandbox Code Playgroud)
然而,如果我以这种方式删除事件它的工作原理:
(...)
bind : function(el, cb) { …Run Code Online (Sandbox Code Playgroud) 嗨,我对以下代码感到困惑
JSfiddle 链接
for(var i=6;i>=0;i--)
{
setTimeout((function(i)
{
$("div").delay(4000).html(i);
alert(i); //if commented unable to see the countdown
})
(i),4000);
//alert(1);
}
Run Code Online (Sandbox Code Playgroud)
我无法得到倒数计时器..什么是错误的...当"警报"被评论时无法看到倒计时数字.请有人解释上面的代码如何工作..发布一些正确的代码
javascript jquery javascript-events settimeout countdowntimer
我正在用javaScript编写我的第一个代码.
我想做这个 :-
我的代码是这样的,
<!DOCTYPE html>
<html>
<head>
<title>iRock - First Project</title>
<script type = "text/javascript">
function touchRock(){
var userName = prompt ("what is your name?","Enter your name here.");
if(userName){
alert("It is good to meet you, " "+userName+ " ".");
document.getElementById("firstImg").src="1.jpg";
}
}
</script>
</head>
<body onload = "alert('hello, I am your pet.');">
<div style = "margin-top:100px; text-align:centre">
<img id="firstImg" src="http://www.smiley-faces.org/wallpaper/smiley-face-wallpaper-widescreen-001.jpg" alt="iRock" style="cursor:pointer" onclick="touchRock()" />
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这有什么问题?因为触摸图像后没有调用事件.
在我的应用程序中,我每1000ms调用一个方法来检查文件readyState.以下是我正在使用的代码,
var success=setInterval(""CheckState()"",1000);
function CheckState(){
if($get('businessDownl').document.readyState=="interactive" ||
$get('businessDownl').document.readyState=="complete"){
alert("Great");
clearInterval(success);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码在IE浏览器中工作正常.但在Firefox和Chrome浏览器中失败.我$get('businessDownl').readyState也尝试使用
,它是打印为undefined.有谁能告诉我如何在上面的场景中使用readyState for FF和chrome?
这段代码(取自这篇文章)已经阻止我Error: 'null' is null or not an object在IE8中尝试使用时收到错误消息()preventDefault()
$(document).ready(function () {
$("#submitBtn").on("click", pD(event));
});
function pD(e) {
e = event || window.event;
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作.然而,这篇文章中的评论(第四评论下来)让我担心这可能是一个糟糕的方法.它在某种程度上是可接受的还是不可接受的?评论的语言非常强,所以这让我很好奇.