Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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 如何检查某个元素是否具有属性";名称“;从某事开始,而不是“开始”;“价值”;_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何检查某个元素是否具有属性";名称“;从某事开始,而不是“开始”;“价值”;

Javascript 如何检查某个元素是否具有属性";名称“;从某事开始,而不是“开始”;“价值”;,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我将使用各种数据属性名称,例如“data mo-” 假设我有以下元素: <span data-mo-top-fade-duration="600">Title 1</span> <span data-mo-bottom-fade-duration="600">Title 2</span> <span data-mo-right-fade-duration="600">Title 3</span> <span data-

我将使用各种数据属性名称,例如
“data mo-”

假设我有以下元素:

<span data-mo-top-fade-duration="600">Title 1</span>
<span data-mo-bottom-fade-duration="600">Title 2</span>
<span data-mo-right-fade-duration="600">Title 3</span>
<span data-mo-left-fade-duration="600">Title 4</span>
标题1
标题2
标题3
标题4

我知道如何处理数据属性值以某个值开头的元素,但是如何处理数据属性名称呢?

如果您只想查找给定节点是否具有以特定字符串开头的属性,那么一种方法是:

// node: a single HTMLELement node,
// attr: the string to test against the attribute names:
function hasAttributeStartingWith(node, attr) {

  // here we return the result, using Array.from() to turn
  // the node-map of attributes into an Array, and then
  // Array.prototype.filter() to remove any attributes that
  // do not return a true/truthy result against the supplied
  // assessment:
  return Array.from(node.attributes).filter(function(attributeNode) {
    // attributeNode: a reference to the current attribute-node
    // of the array of attribute-nodes over which we're iterating.

    // here we test to see if the nodeName (the attribute-name)
    // of the attribute-node begins with  the supplied string
    // (held in the 'attr' variable):
    return attributeNode.nodeName.indexOf(attr) === 0;

  // if the filtered array is greater than zero then
  // there are some attributes beginning with the
  // supplied string:
  }).length > 0;
}

// here we convert the nodeList returned from document.querySelectorAll()
// into an Array, using Array.from(), and iterate over those elements
// using Array.prototype.forEach():
Array.from(document.querySelectorAll('span')).forEach(function(span) {
  // 'span': a reference to the current <span> element of the
  // array of <span> elements over which we're iterating.

  // using the function within the 'if' assessment, since it
  // returns a Boolean true/false:
  if (hasAttributeStartingWith(span, 'data-mo')) {

    // using the Element.classList API to add
    // the 'hasAttributeStartingWith' class to
    // the current <span> if the function returns
    // true:
    span.classList.add('hasAttributeStartingWith');
  }

});
.hasAttributeStartingWith{
显示:内联块;
字号:1.5em;
颜色:柠檬黄;
}
标题1
标题2
标题3

标题4
如果您只想查找给定节点是否具有以特定字符串开头的属性,则一种方法如下:

// node: a single HTMLELement node,
// attr: the string to test against the attribute names:
function hasAttributeStartingWith(node, attr) {

  // here we return the result, using Array.from() to turn
  // the node-map of attributes into an Array, and then
  // Array.prototype.filter() to remove any attributes that
  // do not return a true/truthy result against the supplied
  // assessment:
  return Array.from(node.attributes).filter(function(attributeNode) {
    // attributeNode: a reference to the current attribute-node
    // of the array of attribute-nodes over which we're iterating.

    // here we test to see if the nodeName (the attribute-name)
    // of the attribute-node begins with  the supplied string
    // (held in the 'attr' variable):
    return attributeNode.nodeName.indexOf(attr) === 0;

  // if the filtered array is greater than zero then
  // there are some attributes beginning with the
  // supplied string:
  }).length > 0;
}

// here we convert the nodeList returned from document.querySelectorAll()
// into an Array, using Array.from(), and iterate over those elements
// using Array.prototype.forEach():
Array.from(document.querySelectorAll('span')).forEach(function(span) {
  // 'span': a reference to the current <span> element of the
  // array of <span> elements over which we're iterating.

  // using the function within the 'if' assessment, since it
  // returns a Boolean true/false:
  if (hasAttributeStartingWith(span, 'data-mo')) {

    // using the Element.classList API to add
    // the 'hasAttributeStartingWith' class to
    // the current <span> if the function returns
    // true:
    span.classList.add('hasAttributeStartingWith');
  }

});
.hasAttributeStartingWith{
显示:内联块;
字号:1.5em;
颜色:柠檬黄;
}
标题1
标题2
标题3

标题4
如果我做对了,你想检查数据mo,所以我尝试了一些方法来实现这一点

函数getData(索引){ 如果(索引[0].indexOf(“mo”)!=-1) 返回真值 返回错误 } $。每个($(“span”)函数(i,索引){ var objectKey=Object.keys($(this.data()); if(getData(objectKey)){ $(此).addClass(“有效”) }否则{ $(此).addClass(“无效”) } })
。有效{
颜色:绿色
}
.无效{
字体大小:粗体;
颜色:红色;
文字装饰:线条贯穿
}

标题1
标题2
标题3
标题4

标题5
如果我做对了,你想检查数据mo,所以我尝试了一些方法来实现这一点

函数getData(索引){ 如果(索引[0].indexOf(“mo”)!=-1) 返回真值 返回错误 } $。每个($(“span”)函数(i,索引){ var objectKey=Object.keys($(this.data()); if(getData(objectKey)){ $(此).addClass(“有效”) }否则{ $(此).addClass(“无效”) } })
。有效{
颜色:绿色
}
.无效{
字体大小:粗体;
颜色:红色;
文字装饰:线条贯穿
}

标题1
标题2
标题3
标题4

标题5
使用ES6的一种简明方法是选择文档中的所有元素,然后
。根据是否
筛选
。某些属性名称以您要查找的字符串开头:

const moElements=[…document.querySelectorAll('*')]
.filter(elm=>[…elm.attributes])。一些(
({name})=>name.startsWith('data-mo-'))
));
控制台日志(moElements)
标题1
标题2
标题3
标题4

somethingElse
使用ES6的一种简明方法是选择文档中的所有元素,然后选择
。根据是否
筛选
。某些属性名称以您要查找的字符串开头:

const moElements=[…document.querySelectorAll('*')]
.filter(elm=>[…elm.attributes])。一些(
({name})=>name.startsWith('data-mo-'))
));
控制台日志(moElements)
标题1
标题2
标题3
标题4

somethingElse
太好了,你的代码在哪里?请发布你迄今为止尝试过的代码。你还没想到@苏达尔桑。。等等。问题可能重复更新了。太好了,你的代码在哪里?请发布你迄今为止尝试过的代码。你不明白吗@苏达尔桑。。等待。问题的可能重复项已更新。
array。某些(f)
应比
array.filter(f).length>0
更有效。另外,
string.startsWith(“某物”)
而不是
string.indexOf(“某物”)==0
。(甚至可能
[].some.call(node.attributes,f)
如果可以;未经测试。)
array.some(f)
应该比
array.filter(f).length>0更有效。另外,
string.startsWith(“某物”)
而不是
string.indexOf(“某物”)==0
。(甚至可能
[].some.call(node.attributes,f)
如果可以的话;未经测试)非常感谢,这确实是一段漂亮的代码:)非常感谢,这确实是一段漂亮的代码:)