Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.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或JS在变量中保留元素的每个CSS速记属性_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 使用jQuery或JS在变量中保留元素的每个CSS速记属性

Javascript 使用jQuery或JS在变量中保留元素的每个CSS速记属性,javascript,jquery,html,css,Javascript,Jquery,Html,Css,例如,我为元素指定了一个border-radius CSS属性,比如: <div class="element"></div> 我不想实现的是变量,每个变量都包含一个CSS边框半径值您可以使用拆分: var arrValue = $('.element').css('border-radius').split(' '); 这将为您提供一个数组: [0px',0px',15px'] 只需从元素中提取borderTopLeftRadius、borderTopRightR

例如,我为元素指定了一个border-radius CSS属性,比如:

<div class="element"></div>
我不想实现的是变量,每个变量都包含一个CSS边框半径值

您可以使用拆分:

 var arrValue = $('.element').css('border-radius').split(' ');
这将为您提供一个数组:

[0px',0px',15px']


只需从元素中提取
borderTopLeftRadius
borderTopRightRadius
BorderBorderBottomLeftRadius
BorderBorderBottomRightRadius
值。根本不需要拆分任何数组

本机JavaScript 可通过拉动
顶部
左侧
右侧
底部的单独
边界半径
值来提取这些值:

var el = document.querySelectorAll('.element')[0],
    style = window.getComputedStyle(el);
现在,您可以使用以下方法简单地导出值:

// Top left:
style.borderTopLeftRadius;

// Top right:
style.borderTopRightRadius;

// Bottom left:
style.borderBottomLeftRadius;

//Bottom right:
style.borderBottomRightRadius;

jQuery jQuery中的类似概念:

var el = $('.element');

// Top left:
el.css('borderTopLeftRadius');

// Top right:
el.css('borderTopRightRadius');

// Bottom left:
el.css('borderBottomLeftRadius');

//Bottom right:
el.css('borderBottomRightRadius');


您可以从元素中提取“border radius”的值,然后自己分离字符串。@iacobalin乐意帮忙!:)
console.log({
    topLeft: style.borderTopLeftRadius,
    topRight: style.borderTopRightRadius,
    bottomLeft: style.borderBottomLeftRadius,
    bottomRight: style.borderBottomRightRadius
});

> {topLeft: "0px", topRight: "0px", bottomLeft: "0px", bottomRight: "15px"}
var el = $('.element');

// Top left:
el.css('borderTopLeftRadius');

// Top right:
el.css('borderTopRightRadius');

// Bottom left:
el.css('borderBottomLeftRadius');

//Bottom right:
el.css('borderBottomRightRadius');
console.log({
    topLeft: el.css('borderTopLeftRadius'),
    topRight: el.css('borderTopRightRadius'),
    bottomLeft: el.css('borderBottomLeftRadius'),
    bottomRight: el.css('borderBottomRightRadius')
});

> {topLeft: "0px", topRight: "0px", bottomLeft: "0px", bottomRight: "15px"}