要判断 span 元素是否具有自定义属性,可以使用 JavaScript 中的 getAttribute() 方法。以下是一个例子:

HTML 代码:

<span id='mySpan' data-custom='true'>这是一个 span 元素</span>

JavaScript 代码:

var span = document.getElementById('mySpan');
var customAttr = span.getAttribute('data-custom');

if (customAttr) {
  console.log('span 具有自定义属性');
} else {
  console.log('span 没有自定义属性');
}

在上面的例子中,首先使用 getElementById() 方法获取 span 元素。然后使用 getAttribute() 方法获取名为'data-custom' 的自定义属性的值。如果自定义属性存在,customAttr 的值将为 true,表示 span 具有自定义属性。否则,customAttr 的值将为 null,表示 span 没有自定义属性。根据 customAttr 的值,可以进行相应的判断和操作。

如何用 CSS 判断 span 元素是否具有自定义属性

原文地址: https://www.cveoy.top/t/topic/m3ZQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录