Javascript 带Sitecore页面编辑器的Jquery

Javascript 带Sitecore页面编辑器的Jquery,javascript,jquery,sitecore,sitecore-dms,page-editor,Javascript,Jquery,Sitecore,Sitecore Dms,Page Editor,我需要在一行中的第一个单词周围放置span标记。我使用了下面的Jquery,它在sitecore标准模式下运行良好 $("body").has(".widget h2").addClass("standard-mode"); $("body").has(".scLoadingIndicatorInner").removeClass("standard-mode").addClass("page-edit"); $('.standard-mode .widget h2').html(funct

我需要在一行中的第一个单词周围放置span标记。我使用了下面的Jquery,它在sitecore标准模式下运行良好

$("body").has(".widget h2").addClass("standard-mode");
$("body").has(".scLoadingIndicatorInner").removeClass("standard-mode").addClass("page-edit");


$('.standard-mode .widget h2').html(function(i, html){
    return html.replace(/(\w+\s)/, '<span>$1</span>')
})
$(“body”).has(“.widget h2”).addClass(“标准模式”);
$(“body”).has(“.scLoadingIndicatorInner”).removeClass(“标准模式”).addClass(“页面编辑”);
$('.standard mode.widget h2').html(函数(i,html){
返回html.replace(/(\w+\s)/,“$1”)
})
当我第一次加载页面编辑器时,它也可以正常工作(不插入span标记),但是当我创建一个多元测试时,通过使用箭头标记在a/B之间切换,就像插入span标记一样,它不应该像在页面编辑模式下那样做,但它确实这样做了,并且破坏了html,如下所示

<h2>
        &lt;<span>input </span>id="fld_D26C954B73BE4C62B6F25BE191A86F18_7B55B5E5EDD84D4E88B16C6E073495A5_en_1_0e80a9c63ab6419f8135b70511e892f1_16487" class="scFieldValue" name="fld_D26C954B73BE4C62B6F25BE191A86F18_7B55B5E5EDD84D4E88B16C6E073495A5_en_1_0e80a9c63ab6419f8135b70511e892f1_16487" type="hidden" value="Badger Cull"&gt;<span class="scChromeData">{"commands":[{"click":"chrome:common:edititem({command:\"webedit:open\"})","header":"Edit the related item","icon":"/temp/IconCache/SoftwareV2/16x16/cubes_blue.png","disabledIcon":"/temp/cubes_blue_disabled16x16.png","isDivider":false,"tooltip":"Edit this item in the Content Editor.","type":"common"},{"click":"chrome:rendering:personalize({command:\"webedit:personalize\"})","header":"Personalize","icon":"/temp/IconCache/PeopleV2/16x16/users3_edit.png","disabledIcon":"/temp/users3_edit_disabled16x16.png","isDivider":false,"tooltip":"Personalize component.","type":"sticky"},{"click":"javascript:Sitecore.PageModes.PageEditor.postRequest('ActiveISPageEditor:publish(id={D26C954B-73BE-4C62-B6F2-5BE191A86F18})',null,false)","header":"Publish the related item","icon":"/temp/IconCache/Network/16x16/download.png","disabledIcon":"/temp/download_disabled16x16.png","isDivider":false,"tooltip":"Publish this item.","type":"common"},{"click":"chrome:rendering:editvariations({command:\"webedit:editvariations\"})","header":"Edit variations","icon":"/temp/IconCache/SoftwareV2/16x16/breakpoints.png","disabledIcon":"/temp/breakpoints_disabled16x16.png","isDivider":false,"tooltip":"Edit the variations.","type":"sticky"}],"contextItemUri":"sitecore://master/{D26C954B-73BE-4C62-B6F2-5BE191A86F18}?lang=en&amp;ver=1","custom":{},"displayName":"Header Text","expandedDisplayName":null}</span><span scfieldtype="single-line text" sc_parameters="prevent-line-break=true" contenteditable="false" class="scWebEditInput scEnabledChrome" id="fld_D26C954B73BE4C62B6F25BE191A86F18_7B55B5E5EDD84D4E88B16C6E073495A5_en_1_0e80a9c63ab6419f8135b70511e892f1_16487_edit" sc-part-of="field">Badger Cull</span>
    </h2>

输入id=“fld_D26C954B73BE4C62B6F25BE191A86F18_7b55b5edd84d4e88b16c6e0734955a5_en_1_0e80a9c63ab6419f8135b70511e892f1_16487”class=“scFieldValue”name=“fld__d26c954; b954b734; b734b5b5b191a86f18_7b55e5edd84d4e88b16; en_en_1; 1_en_1; 1; 1; 1; 80a8; 1; 80a8; c6a4a6a6e7b734a5e5e5e4e4e4e4e4e41; 1a8)1a8(1; 1a8({command:\'webedit:open\'”,“header”:“编辑相关项”,“图标”:“/temp/IconCache/SoftwareV2/16x16/cubes\u blue.png”,“disabledIcon”:“/temp/cubes\u blue\u disabled16x16.png”,“isDivider”:false,“工具提示”:“在内容编辑器中编辑此项”。,“键入”:“常用”},{“单击”:“chrome:rendering:personalize({command:\“webedit:personalize\”),“header”:”“Personalize”,“icon”:“/temp/IconCache/PeopleV2/16x16/users3_edit.png”,“disabledIcon”:“/temp/users3_edit_disabled16x16.png”,“isDivider”:false,“tooltip”:“Personalize component.”,“type”:“sticky”},{“click”:“javascript:Sitecore.PageModes.PageEditor.postRequest('ActiveISPageEditor:publish(id={D26C954B-73BE-4C62-B6F2-5BE191A86F18})”,null,false),“header”“:“发布相关项目”,“图标”:“/temp/IconCache/Network/16x16/download.png”,“disabledIcon”:“/temp/download\u disabled16x16.png”,“isDivider”:false,“工具提示”:“发布此项目”。,“键入”:“common”},{“click”:“chrome:rendering:editvariations({command:\“webedit:editvariations\”,“header”:“编辑变体”,“图标”:”“/temp/IconCache/SoftwareV2/16x16/breakpoints.png”,“disabledIcon”:“/temp/breakpoints_disabled16x16.png”,“isDivider”:false,“工具提示”:“编辑变体”,“键入”:“sticky”}],“contextItemUri”:”sitecore://master/{D26C954B-73BE-4C62-B6F2-5BE191A86F18}?lang=en&;ver=1,“自定义”:{},“显示名”:“标题文本”,“扩展显示名”:null}Badger Cull

任何建议都会有帮助。

您可以检查JavaScript属性
Sitecore.PageModes.PageEditor
,查看您是否处于页面编辑器模式,并相应地禁用JavaScript

function isPageEditor() {
    if (typeof Sitecore == "undefined") {
        return false;
    }
    if (typeof Sitecore.PageModes == "undefined" || Sitecore.PageModes == null) {
        return false;
    }
    return Sitecore.PageModes.PageEditor != null;
}

if (isPageEditor() == false) {
    //do your stuff here   
}

+1一直以来,我都在主体上设置css类,并以这种方式禁用!非常好的信息。很抱歉,我不是javascript专家,在使用上述代码后,我最终遇到了错误ReferenceError:Sitecore未定义if(Sitecore==null){我尝试了您提供的解决方案,它在正常模式和页面编辑器模式下运行良好,但是当我尝试切换多变量测试的控件时,它仍然插入span标记,这不应该像在页面编辑器模式下那样?有什么建议吗?这对我使用Sitecore 8.1有帮助,在Sitecore 8.1中,我们需要加载Marketo表单,但在中编辑时失败这个人说。