标签: javascript-events

Backbone - 视图事件对象中的自定义事件

我试图找出其工作原理:

var TestView = Backbone.View.extend({
    views: {},

    initialize: function(){
        this.bind('transistion:complete', this.onPageLoaded);

    },

    onPageLoaded: function(){
        alert("works!");
    }
});

var v = new TestView();
v.trigger('transition:complete');
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

var TestView = Backbone.View.extend({
    views: {},

    events:{
        'transition:complete': 'onPageLoaded'
    },

    onPageLoaded: function(){
        alert("works!");
    }
});

var v = new TestView();
v.trigger('transition:complete');
Run Code Online (Sandbox Code Playgroud)

能够从视图的事件对象中触发我的自定义事件更简洁.无论如何我可以让这个工作吗?

javascript-events backbone.js

1
推荐指数
2
解决办法
6622
查看次数

Cross Domain Post方法使用jquery和xml响应调用ajax

我想使用post方法发送一个ajax请求,xml作为响应文本,是否可能,如果有可能请告诉我可能的方法.

对于Ex

url : "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate"
data : {FromCurrency:"INR",ToCurrency:"AUD"}
method : GET or POST
Run Code Online (Sandbox Code Playgroud)

http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=AUD

我需要使用ajax来响应这个url.

javascript ajax jquery javascript-events

1
推荐指数
1
解决办法
5626
查看次数

Javascript中的函数链

在javascript中,我们以准并行方式触发函数

window.onload=function(){
document.getElementById("test").addEventListener('click', function1(), false);
//consider it takes 3 seconds to be completed
document.getElementById("test").addEventListener('click', function2(), false);
}
Run Code Online (Sandbox Code Playgroud)

我们如何能火function2()当function1()执行完毕?

在jQuery中,我们可以链接一系列函数(例如):

$(this).fadeIn(3000).fadeOut(2000);
Run Code Online (Sandbox Code Playgroud)

如何在纯JavaScript中进行此功能更改?

编辑:在回应否定评论和投票时,我提供了这个例子:

function delay(time, func){
setTimeout(func,time);
}

delay(2000,function(){alert('Function 1');});
delay(1000,function(){alert('Function 2');});
Run Code Online (Sandbox Code Playgroud)

在此示例中,您将首先看到"功能2"的警报.

javascript javascript-events

1
推荐指数
1
解决办法
3638
查看次数

如何从左向右移动/滑动图像

我想从左到右滑动或移动图像

http://rajeevkumarsingh.wix.com/pramtechnology

阅读五角形框,移动好!

我尝试了一下但没有这样做我使用下面的代码

<script type="text/javascript">

<!--
var imgObj = null;
var animate ;
function init(){
   imgObj = document.getElementById('myImage');
   imgObj.style.position= 'absolute'; 
   imgObj.style.top = '240px';
   imgObj.style.left = '-300px';
   imgObj.style.visibility='hidden';
   moveRight();
} 
function moveRight(){
if (parseInt(imgObj.style.left)<=10)
{
   imgObj.style.left = parseInt(imgObj.style.left) + 5 + 'px';
   imgObj.style.visibility='visible';
   animate = setTimeout(moveRight,20); // call moveRight in 20msec
   //stopanimate = setTimeout(moveRight,20);
  }
else
  stop();
  f();
}

function stop(){
   clearTimeout(animate);
}
window.onload =init;
//-->
</script>
<img id="myImage" src="xyz.gif" style="margin-left:170px;" />
Run Code Online (Sandbox Code Playgroud)

Firefox和IE也存在某种解决问题.如何解决它们.此外,我无法如此清楚地移动这些东西.这可能吗?我希望它与javascript而不是闪存.

javascript javascript-events motion

1
推荐指数
1
解决办法
6万
查看次数

在Javascript中获取关联数组的索引

所以我有一组"关联数组",即.对象,包含各种HTML元素的数据和其他一个参数,如下所示:

container[0] = { picker_canvas: document.getElementById("background_picker"),
                 color_canvas: document.getElementById("background_color"),
                 hex_text: document.getElementById("background_text"),
                 mouse_down: false };

container[1] = { picker_canvas: document.getElementById("textbox_picker"),
                 color_canvas: document.getElementById("textbox_color"),
                 hex_text: document.getElementById("textbox_text"),
                 mouse_down: false };

container[2] = { picker_canvas: document.getElementById("font_picker"),
                 color_canvas: document.getElementById("font_color"),
                 hex_text: document.getElementById("font_text"),
                 mouse_down: false };
Run Code Online (Sandbox Code Playgroud)

每个容器都有一个颜色选择器画布,一个颜色预览画布,一个显示十六进制颜色值的文本框和一个mouse_down布尔值的引用.稍后我通过遍历容器来初始化一些事件监听器,如下所示:

for (i=0; i<3; i++) {

    container[i].picker_canvas.addEventListener("mousedown", function() {
        container[i].mouse_down = true;
    }, false);

    container[i].picker_canvas.addEventListener("mouseup", function() {
        container[i].mouse_down = false;
    }, false);

    container[i].picker_canvas.addEventListener("mousemove", function(evt) {
        getColor(container[i], evt);
    }, false);

    container[i].hex_text.addEventListener("change", function(evt) {
        drawColorSquare(container[i], evt.target.value)
    }, false);

}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为我变得未定义,所以我现在试图通过做这样的事情来获得正确的索引,但我不知道如何在javascript中实际实现它

container[i].picker_canvas.addEventListener("mousedown", function(evt) {
    container[container.indexof(evt.target)].mouse_down = …
Run Code Online (Sandbox Code Playgroud)

javascript arrays indexing associative-array javascript-events

1
推荐指数
1
解决办法
479
查看次数

Javascript中的addEventListener会自动触发click事件

我想制作一个简单的javascript倒计时程序.由于某种原因倒计时触发,我不必单击开始链接.

<!DOCTYPE HTML>
<html>
    <head>
        <title id = "titleElement">My Top Ten Movies</title>

    </head>

    <body>

        <p>Countdown Suren's favorite ten movies
            <a href="" id = "start"> Start </a>
        </p>

        <table>

            <thead>
                <tr>
                    <td>Number</td>
                    <td>Movie</td>
                </tr>   
            </thead>

            <tbody id = "movieList">

            </tbody>

        </table>

    <script src="js/practice.js"></script>  
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

和我的JavaScript文件......

//get the start element
var start = document.getElementById("start");

//register listener
start.addEventListener('click', startCountDown(), false);


function startCountDown()
{
    //alert("Here!");
    //creat the list of 10 movies
    var myTenMovies = ["Breaveheart", "Mars Attacks", "Startship Troopers", "Jurassic Park", "Terminator 2", …
Run Code Online (Sandbox Code Playgroud)

javascript javascript-events

1
推荐指数
1
解决办法
4860
查看次数

文档就绪v/s正文就绪v/s窗口就绪事件

这三个事件之间有什么不同?

哪一个在其他之前/之后加载?

   <body> 
        <script type='text/javascript'> 
            $(document).ready(function(){
                console.log('Document %s',+new Date());
            });
            $('body').ready(function(){
                console.log('Body %s',+new Date());
            });
            $(window).ready(function(){
                console.log('Window %s',+new Date());
            }); 
        </script>
        <div>hello world</div>
    </body>
Run Code Online (Sandbox Code Playgroud)

奇怪的是,它们与我将它们放在代码上的顺序相同.对于当前的例子.document一个先发射,windows另一个发射.

ps我读过window.onload vs <body onload =""/>, window.onload vs document.onload和其他几个.

javascript jquery event-handling javascript-events

1
推荐指数
1
解决办法
922
查看次数

页面加载后的JQuery(窗口).load?

当这个监听器在事件发生后添加时,会JQuery运行传入的函数吗?$(window).load(callback)window.load

javascript jquery javascript-events

1
推荐指数
2
解决办法
9571
查看次数

在特定情况下,无法从类的函数内访问类的变量

我不知道如何最好地解释这个问题,因为有很多事情要发生,所以我继续创造了一个我正在做的样本.以下是代码:

var cl = new Class();
cl.handleAction("triggerScream");
cl.handleAction('triggerDisplay');



function Class()
{
  //function which will print static string
  this.scream = function(){
    document.write("AAAAAAA!!!<br/>");
  }


  //function which will print the class variable
  this.display = function(){
    document.write( this.color );
  };


  //sample class variable
  this.color = 'red';


  //map of actions
  this.actions = {
    'triggerDisplay' : this.display,
    'triggerScream' : this.scream
  };


  //generic function that handles all actions
  this.handleAction = function(action){
    try{
      this.actions[action]();
    }catch(err)
    {
      document.write("Error doing "+action);
    }
  };
}
Run Code Online (Sandbox Code Playgroud)

这里是jsbin链接:http://jsbin.com/etimer/2/edit

在摘要中,有一个handleAction()函数,它处理各种事件并唤起其他函数来完成事件.为此,我有动作事件和功能的地图来唤起. …

javascript jquery javascript-events

1
推荐指数
1
解决办法
127
查看次数

window.unload在IE8中不起作用

我想unload在父窗口打开的子窗口上执行javascript .我尝试了下面的代码,它没有调用该函数.

 childWindow = window.open(url, 'MyWindow', GetWindowOptions(1020, 600), true);
 childWindow.onunload = function () { test(); };
Run Code Online (Sandbox Code Playgroud)

我写的测试功能是:

function test() {
alert(1);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用IE8浏览器.

javascript javascript-events

1
推荐指数
1
解决办法
4919
查看次数