繁体   English   中英

给列表中的每个数字加1

[英]add 1 to every number in the list

在Dreamweaver中,我有很长的清单,看起来像这样:

.classname li:nth-of-type(1) {...}
.classname li:nth-of-type(23) {...}
.classname li:nth-of-type(111) {...}

等等。 我现在需要做的是在每个li:nth-​​of-type选择器中添加1,因此它变为:

.classname li:nth-of-type(2) {...}
.classname li:nth-of-type(24) {...}
.classname li:nth-of-type(112) {...}

我试图通过正则表达式的搜索和替换功能来实现此目的,但由于添加而无法正常工作。

什么是最简单的方法来做到这一点?

我所知道的没有内置的方法可以执行您要执行的操作。您将必须在Dreamweaver中手动进行更改。

虽然我擅长JavaScript,但您可以使用Tom Muck的“评估JavaScript”面板( http://www.communitymx.com/abstract.cfm?cid=270FB商业广告,但只需2美元,我在CS5中并不厌倦.5或6,但肯定可以在CS5中使用,并且应该在更高版本中使用)或Dreamweaver Platform SDK( http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1009962 #虽然它说DW8和MX2004应该可以在更高版本中使用,但我确定我已经将其安装到CS3中,并且刚刚在CS6中进行了测试,并且安装得很好,只是一个小问题,其中添加了几个添加的菜单项命令->其他菜单),其中包含可以输入JavaScript并运行的命令。

那么,为什么在这里提到JavaScript? 好吧,Dreamweaver的可扩展性层是通过公开JavaScript API构建的。 这意味着您可以使用JavaScript处理文档。 在这种情况下,请编辑文档以增加编号。

我刚刚使用Dreamweaver Platform SDK评估JavaScript命令在Dreamweaver CS6中测试了以下内容。

在代码视图中选择要增加的CSS选择器。 转到命令-> SDK工具->评估JavaScript。 将以下代码粘贴到您的文档中:

var dom = dw.getDocumentDOM();
var sel = dom.source.getSelection();
var src = dom.source.getText(sel[0], sel[1]);

var matches = src.match(/(\.classname li:nth-of-type\()(\d+)(\))/g);
var newSrc = src;
if(matches){
    for(var i =0; i< matches.length; i++){
      // note: the following code through the ending ; is all on one line
      newSrc = newSrc.replace( matches[i], matches[i].replace(/(\.classname li:nth-of-type\()(\d+)(\))/, function(str, p1, p2, p3){return p1 + (parseInt(p2)+1) + p3} ) );
    }
}

dom.source.replaceRange(sel[0], sel[1], newSrc);

单击评估按钮。 您应该在代码中看到数字递增。

注意:此代码使用正则表达式查找您提供的特定CSS选择器,因此,如果您有不同的CSS选择器,则需要在src.match()行以及newSrc.replace( )行。

为了使其更加通用,您可能需要尝试以下操作:

var dom = dw.getDocumentDOM();
var sel = dom.source.getSelection();
var src = dom.source.getText(sel[0], sel[1]);

var matches = src.match(/(\()(\d+)(\))/g);
var newSrc = src;
if(matches){
    for(var i =0; i< matches.length; i++){
        // note: the following code through the ending ; is all on one line
        newSrc = newSrc.replace( matches[i], matches[i].replace(/(\()(\d+)(\))/, function(str, p1, p2, p3){return p1 + (parseInt(p2)+1) + p3} ) );
    }
}

dom.source.replaceRange(sel[0], sel[1], newSrc);

这将简单地替换与括号括起来的数字匹配的任何文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM