【问题标题】:add 1 to every number in the list为列表中的每个数字加 1
【发布时间】:2012-06-15 07:02:33
【问题描述】:

在 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) {...}

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

最简单的方法是什么?

【问题讨论】:

    标签: list replace numbers dreamweaver


    【解决方案1】:

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

    虽然我你擅长 JavaScript,但你也许可以使用 Tom Muck 的 Evaluate JavaScript 面板(http://www.communitymx.com/abstract.cfm?cid=270FB 商业版,但只需 2 美元我在 CS5.5 或 6 中并没有厌倦它,但它肯定在CS5,应该在更高版本中工作),或 Dreamweaver 平台 SDK(http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1009962# 虽然它说 DW8 和 MX2004 它应该在更高版本中工作,我确定我已经将它安装到 CS3 中,并且刚刚在 CS6 中进行了测试安装得很好,只是一个小问题,其中一些添加的菜单项被放置在 Commands -> Miscellaneous menu 上),其中包括一个可以输入 JavaScript 并运行的命令。

    那么,为什么要在这里提到 JavaScript?嗯,Dreamweaver 的可扩展层是在构建时公开了一个 JavaScript API。这意味着您可以使用 JavaScript 操作文档。在这种情况下,请编辑文档以增加数字。

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

    在代码视图中选择要增加的 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 中的 RegExp .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);
    

    这只是替换与括号括起来的数字匹配的任何文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2012-07-29
      • 2021-02-12
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多