Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.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如何将正则表达式css样式从正到负,从负到正?_Javascript_Css_Regex - Fatal编程技术网

Javascript jquery如何将正则表达式css样式从正到负,从负到正?

Javascript jquery如何将正则表达式css样式从正到负,从负到正?,javascript,css,regex,Javascript,Css,Regex,jquery如何将正则表达式css样式从正到负,从负到正 var aaa='<p style="top:10px;left:-10px;">text</p>'; aaa.attr('style').replace(/\-/gi,'').replace(/\d+/gi,'-$&');//this will replace negative to positive, then positive to negative, so nothing change 然而,风格

jquery如何将正则表达式css样式从正到负,从负到正

var aaa='<p style="top:10px;left:-10px;">text</p>';
aaa.attr('style').replace(/\-/gi,'').replace(/\d+/gi,'-$&');//this will replace negative to positive, then positive to negative, so nothing change
然而,风格可以是其他规则。另一个示例效果:

top:-100px;left:200px; => top:100px;left:-200px;

正如Rooster所评论的,使用一个将匹配的数字乘以-1的函数。函数的返回值用作替换字符串

var aaa='<p style="top:10px;left:-10px;">text</p>';
aaa.replace(/[-\d]+/g, function(x) { return -parseInt(x); })
// => "<p style="top:-10px;left:10px;">text</p>"
var aaa='

text

; replace(/[-\d]+/g,函数(x){return-parseInt(x);}) //=>“

文本


使用jQuery,您可以这样做:

$(aaa).css('left', function (i, value) {
    return -parseInt(value);
});

这是一个演示(单击红色框):。

我知道你的问题是“jquery如何将css样式从正到负,从负到正?”不知道你想要实现什么,但也许这里有一个css解决方案

$('.content').click(function () {
$(this).toggleClass("left");
});


问候

用负数乘以就行了?你能详细说明一下吗?这似乎是一个奇怪的操作,你真的需要正则表达式吗?
$('.content').click(function () {
$(this).toggleClass("left");
});