JavaScript |我需要一些关于函数的建议
如何使用JavaScript检查div上的样式,如果边界半径等于0%或100%,则它会执行某些操作 例如,我将如何做这样的事情。 这就是我认为它应该看起来的样子JavaScript |我需要一些关于函数的建议,javascript,html,css,Javascript,Html,Css,如何使用JavaScript检查div上的样式,如果边界半径等于0%或100%,则它会执行某些操作 例如,我将如何做这样的事情。 这就是我认为它应该看起来的样子 if (border radius = 0%) { starts a loader animation } 做这样的事 if (window.getComputedStyle(document.getElementById("abc")).borderRadius === "100%") { } 以下是检索样式、比较边界半径和条件
if (border radius = 0%) {
starts a loader animation
}
做这样的事
if (window.getComputedStyle(document.getElementById("abc")).borderRadius === "100%") {
}
以下是检索样式、比较边界半径和条件的示例:
// this function allows to retrieves CSS properties related to border radius
function getBordersRadius(a) {
return {
topLeft: window.getComputedStyle(a)['border-top-left-radius'],
topRight: window.getComputedStyle(a)['border-top-right-radius'],
bottomRight: window.getComputedStyle(a)['border-bottom-right-radius'],
bottomLeft: window.getComputedStyle(a)['border-bottom-left-radius']
}
}
// this function checks if all border radius are the same
function isAllBorderRadius(a, b) {
var elBorderRadius = getBordersRadius(a);
var elBorderRadiusSame = true;
for (var i in elBorderRadius) {
if (elBorderRadius[i] !== b) {
elBorderRadiusSame = false;
}
}
return elBorderRadiusSame;
}
// you can then use the condition
if (isAllBorderRadius(document.getElementById('ELEMENT_ID'), '100%')) {
// ...
}
欢迎@AnthonyL。如果对您有效,请接受此答案:)不起作用,
边框半径
是四个不同值(边框左下半径、边框右下半径、边框左上半径、边框右上半径)的快捷方式,它没有存储到CSSStyleDeclaration对象。@Nanditarorasharma没有很好地工作,出现了一个错误。您得到了什么错误?您可以吗elaborate@NanditaAroraSharma错误为未捕获类型错误:未能在“窗口”上执行“getComputedStyle”:参数1不是“元素”类型。您创建的getBordersRadius函数未正常工作并出现错误。core.js:8未捕获类型错误:未能在“窗口”上执行“getComputedStyle”:参数1不是“元素”类型。在GetBordersRadius,您必须定义一个有效的元素来代替“document.getElementById('element_ID')”。要检查样式的元素在示例中必须具有设置为“element\u id”的“id”属性。例:测试