标签: javascript-events

Javascript中的年份验证

我想做年份验证,它应该只是数字,它应该是4位数长,在1920年到当前年份之间我已经创建了java脚本函数,如下所示

function yearValidation(year) {
    var text = /^[0-9]+$/;
    if (year != 0) {
        if ((year != "") && (!text.test(year))) {

            alert("Please Enter Numeric Values Only");
            return false;
        }

        if (year.length != 4) {
            alert("Year is not proper. Please check");
            return false;
        }
        var current_year=new Date().getFullYear();
        if((year < 1920) || (year > current_year))
            {
            alert("Year should be in range 1920 to current year");
            return false;
            }
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

并打电话给它 onkeypress="return yearValidation(this.value)"

但当我输入1时,它会让我警觉 Year should be in range 1920 to current …

javascript validation javascript-events

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

为什么javascript函数调用函数名后不包含'()'?

window.onload = function() {
  document.getElementById('clickMe').onclick = runTheExample;
}

function runTheExample() {
  alert('running the example');
}
Run Code Online (Sandbox Code Playgroud)

对于id = clickMe的html输入按钮,这是onclick事件的简单事件处理程序.

在第2行,为什么函数调用runTheExample不会紧跟()?我认为要调用一个函数,你必须在开/关括号中传递它所需的任何变量/对象,如果函数不期望任何东西,你必须仍然包括开括号和右括号runTheExample().

javascript syntax javascript-events

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

在JQuery方法中设置匿名函数的'this'上下文

我想通过以下单击绑定方法传递特定上下文:

$(document).ready(function(){

    var o = {
        name : 'I\'m object O'
    };

    $('#my-button').click(function(){
        alert(this.name);
    }.call(o));
});
Run Code Online (Sandbox Code Playgroud)

问题是,只要页面加载就会触发此操作.有没有办法可以将上下文绑定到click处理程序的匿名函数,并且只有在触发click事件时才触发它?你也可以解释为什么它是在页面加载而不是点击事件上触发的.

javascript jquery this javascript-events

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

在Prototype上使用addEventListener

我试图通过点击addEventListener来破坏Prototype的结果,但结果显示甚至没有点击.

<div id="box">Your Item:</div>

<button id="myBtn">Buy</button>

<script type="text/javascript">
function Machine(n,p){
this.name = n;
this.price = p;
}


Machine.prototype.Dispatch = function(){
var container = document.getElementById('box');
container.innerHTML = this.name + " " + this.price;
}
var Drink = new Machine("coke",80);
var Handle = document.getElementById('myBtn');
Handle.addEventListener("click",Drink.Dispatch(),false);
</script>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/9trqK/

javascript javascript-events

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

如何找到'this'节点的'父'?

在定位当前节点时,您可以event="function(this)"告诉您正在处理此节点的函数.如何获得当前元素所在的节点?

<div>
    <span event="function(this)">Click here</span>
</div>
Run Code Online (Sandbox Code Playgroud)

我想处理的是div,而不是span.目前的代码涉及到span.

javascript dom javascript-events

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

这个javascript/HTML出了什么问题

我想我只需要第二双眼睛.div的onclick事件似乎不起作用.有任何想法吗?

<!DOCTYPE html> 
<html> 
 <head>
 <meta charset="UTF-8">
 <title="My First Program"/>

 <script type="text/javascript">
      window.onload = function(){
           window.alert("If you see me then the page has loaded"); 
           click(); 
      }

      //we do programming here 
      /*because
        it is 
        fun*/
      window.alert("Helo World!"); 

      function click(){
           window.alert("CLICK!!!!");
      }


 </script>

 </head>
 <body>
      <div>This web page will run my first program</div>
      <!--this will be awesome-->
      <br>
      <br>
      <br>
      <div id="d1" onclick="click()">Click me</div>
 </body>
Run Code Online (Sandbox Code Playgroud)

另外,对于记录,这不是我的第一个程序.

javascript javascript-events

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

为什么事件委托不使用jquery on()

我正在使用最新的jquery,它说不.live()推荐使用,.on()应该使用它.我将点击事件附加到按钮时遇到问题.我动态修改按钮值,应该能够处理这两种情况

<input type="text" ><input id="button_1" type="button" value="add" >
<input type="text"> <input id="button_2" type="button" value="add">

$('[id^="button_"]').on("click", "input", function() {
    $(this).val("delete");

    $('#button_'+$(this).attr('id').split('_')[1]).attr('id', 'delButton_'+$(this).attr('id').split[1]);

});

$('[id^="delButton_"]').on("click", "input", function() {
    $(this).val("add");

    $('#delButton_'+$(this).attr('id').split('_')[1]).attr('id', 'button_'+$(this).attr('id').split[1]);

});
Run Code Online (Sandbox Code Playgroud)

这是演示:jsfiddle

javascript jquery javascript-events event-delegation

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

jquery .on()双击传递数据

使用时

$(document).on('dblclick', '#selector_id', {form_key:10}, my_function)

my_function = function(){
    console.log(event)
}
Run Code Online (Sandbox Code Playgroud)

我希望能够检索form_key从event.data,按照文档

但是,在这种情况下,我得到一个MouseEvent,而不是Event,它没有data属性.

我错过了什么?

jquery event-handling javascript-events

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

当鼠标指针在浏览器视口之外时捕获事件

当页面加载时,我正在用javascript写一个简单的无限计数器开始计数

当鼠标指针位于视口之外时,我想停止计数器

请帮忙?

        var i=0;


            setInterval(function (){
               i++;

               document.getElementById("counterLoop").innerHTML=i;

            },1000);

    var viewportWidth  = document.documentElement.clientWidth;
     var viewportHeight = document.documentElement.clientHeight;


     function getCursorXY(e) {   
CurX = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
CurY = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

}
Run Code Online (Sandbox Code Playgroud)

如何捕获视口宽度和高度的鼠标移动事件

javascript jquery javascript-events

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

如何使用JQuery或javascript从动态创建的按钮获取click事件对象数据

我正在收集页面按钮点击事件.通常我是从静态创建的DOM元素中收集对象.通过使用,

 $('input[type=button]').each(function () {
              $(this).bind('click', function () {
                  Console.log(this);
              });
          });
Run Code Online (Sandbox Code Playgroud)

但是当我动态添加一个新按钮时,

 vvar newBtn = document.createElement('input');
      newBtn.type = 'button';
      newBtn.setAttribute('id', 'JgenerateBtn');
      newBtn.setAttribute('value', 'JgenerateBtn');
      newBtn.onclick = function () { alert('javascript dynamically created button'); };
      var holderDiv = document.getElementById('holder');
      holderDiv.appendChild(newBtn);
Run Code Online (Sandbox Code Playgroud)

在此代码之后,创建了New Button并且事件也触发了,但是我无法通过使用相同的上述代码来获取Event对象.

 $('input[type=button]').each(function () {
          $(this).bind('click', function () {
              Console.log(this);
          });
      });
Run Code Online (Sandbox Code Playgroud)

请提供获取动态创建的元素事件对象的建议.

javascript jquery javascript-events

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