Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将动态添加的事件(作为属性)与jQuery匹配_Javascript_Jquery_Dynamic_Jquery Selectors - Fatal编程技术网

Javascript 将动态添加的事件(作为属性)与jQuery匹配

Javascript 将动态添加的事件(作为属性)与jQuery匹配,javascript,jquery,dynamic,jquery-selectors,Javascript,Jquery,Dynamic,Jquery Selectors,如何匹配具有在运行时添加的特定属性的所有元素 例如,代码: $('[onkeydown]') 将成功匹配: <div onkeydown="foo();"> 注意:如果我执行上述相同操作,但不是使用事件(onkeydown),而是使用不同的非事件属性(例如title),它将成功匹配动态添加的属性。因此,这似乎只是事件的问题。[] /** * Returns all the elements with the specified attribute. * @param base i

如何匹配具有在运行时添加的特定属性的所有元素

例如,代码:

$('[onkeydown]')
将成功匹配:

<div onkeydown="foo();">
注意:如果我执行上述相同操作,但不是使用事件(
onkeydown
),而是使用不同的非事件属性(例如
title
),它将成功匹配动态添加的属性。因此,这似乎只是事件的问题。

[]

/**
* Returns all the elements with the specified attribute.
* @param base is optional (root element)
*/
function getElementsByAttr( attr, base ) {
  base = base || document;
  var all = base.getElementsByTagName('*'),
      len = all.length,
      res = [];
  for ( var i = 0; i < len; i++ )
    if ( all[i][attr] != null )
      res.push( all[i] )
  return res;
}
/**
*返回具有指定属性的所有元素。
*@param base是可选的(根元素)
*/
函数getElementsByAttr(属性,基本){
base=base | |文档;
var all=base.getElementsByTagName('*'),
len=所有长度,
res=[];
对于(变量i=0;i

请注意,该函数会检查DOM中的所有元素(如果没有提供根元素),因此在繁重的布局上使用时会变慢。

这是用于事件处理程序,还是用于运行插件/修改元素?@Nick:我不确定我是否完全理解这个问题,但在获得匹配的元素之后,我想用
myElements.removeAttr('onkeydown')
@senseve之类的代码删除它-什么时候添加事件?其他东西正在附加一个
onkeydown
处理程序,您想删除它,对吗?@Nick:正在加载。。。是的,他们身上还有别的东西。。。我无法阻止这种行为。因此,我希望在添加后匹配所有元素。@sensefure-其他函数是否有回调函数等,您可以附加到的任何东西,在完成后运行?这确实有效。我希望有一个jQuery解决方案。虽然jQuery确实匹配元素,但它不会匹配正确的元素,因为
:has
正在查找任何具有该属性的子体。例如,它将始终匹配我的
标记的祖先(例如
),但实际上不会匹配
标记本身。我将使用您的原始代码,它似乎可以完美地工作。谢谢,我不太喜欢jQuery。我应该仔细阅读这些文件。原来的那个还是稳定的。
$('#mydiv').attr('onkeydown') // == foo
/**
* Returns all the elements with the specified attribute.
* @param base is optional (root element)
*/
function getElementsByAttr( attr, base ) {
  base = base || document;
  var all = base.getElementsByTagName('*'),
      len = all.length,
      res = [];
  for ( var i = 0; i < len; i++ )
    if ( all[i][attr] != null )
      res.push( all[i] )
  return res;
}