Javascript SharePoint 2010 - 内容编辑器 Web 部件复制条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5020573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:16:33  来源:igfitidea点击:

SharePoint 2010 - Content Editor Web Part Duplicating Entries

javascriptsharepoint-2010

提问by NakedBrunch

I'm using Javascript inside of a SharePoint 2010 Content Editor Web Part to insert a Silverlight object. I need to do it this way instead of use a Silverlight Web Part because Silverlight Web Parts are not currently enabled. This is done entirely using Javascript.

我在 SharePoint 2010 内容编辑器 Web 部件中使用 Javascript 来插入 Silverlight 对象。我需要这样做而不是使用 Silverlight Web 部件,因为当前未启用 Silverlight Web 部件。这完全是使用 Javascript 完成的。

The problem occurs when I go later to edit the Javascript inside of the CEWP - I can see the orginal Javascript requesting generation of Silverlight object and I can, this is the strange part, the CEWP has all of the generated HTML of the Silverlight object right there appended to the scrept.

当我稍后在 CEWP 中编辑 Javascript 时出现问题 - 我可以看到原始 Javascript 请求生成 Silverlight 对象,我可以,这是奇怪的部分,CEWP 拥有 Silverlight 对象的所有生成的 HTML 权限有附加到 scrept。

So now, when I save, I save the script to generate the Silverlight object AND the HTML that was previously generated effectively duplicating the Silverlight object. If I edit again then I will now have three Silverlight objects and so on.

所以现在,当我保存时,我保存脚本以生成 Silverlight 对象和之前生成的 HTML,有效地复制了 Silverlight 对象。如果我再次编辑,那么我现在将拥有三个 Silverlight 对象,依此类推。

You can see this in action for yourself with the following sample code:

您可以使用以下示例代码亲自查看此操作:

  1. Add a new Content Editor Web Part to a page in SharePoint 2010
  2. Edit the source HTML
  3. Add the following code:

    <script type="text/javascript">document.write("Hello<br/>");</script>

  1. 向 SharePoint 2010 中的页面添加新的内容编辑器 Web 部件
  2. 编辑源 HTML
  3. 添加以下代码:

    <script type="text/javascript">document.write("Hello<br/>");</script>

Save the web part and you're done. Now, just keep editing the CEWP. Every time you click "Edit Web Part", "Hello" will be appended to your script.

保存 web 部件,你就完成了。现在,只需继续编辑 CEWP。每次单击“编辑 Web 部件”时,“Hello”都会附加到您的脚本中。

How can I use Javascript to insert DOM elements and not have the generated HTML appear in the CEWP?

如何使用 Javascript 插入 DOM 元素而不让生成的 HTML 出现在 CEWP 中?

回答by NakedBrunch

It's not working because SharePoint 2010 doesn't want you copying and pasting scripts into the editor. Instead, you should be putting your scripts inside a txt file (yes, that's right a txt file) stored in SharePoint and then pointing the CEWP to use that file as the source.

它不起作用,因为 SharePoint 2010 不希望您将脚本复制并粘贴到编辑器中。相反,您应该将脚本放在存储在 SharePoint 中的 txt 文件(是的,这就是 txt 文件)中,然后指向 CEWP 以使用该文件作为源。

First, create a file with all of your code (both Javascript and HTML - basically everything you would have normally pasted into the content editor.) Make sure to wrap your Javascript in the <script type="text/javascript">tag and save the file with a .txt extension like "scripts.txt".

首先,创建一个包含所有代码的文件(包括 Javascript 和 HTML - 基本上是您通常粘贴到内容编辑器中的所有内容。)确保将您的 Javascript 包装在<script type="text/javascript">标签中,并使用 .txt 扩展名保存文件,例如“scripts 。文本”。

Next, add a CEWP to your page and select "Edit Web Part." In the content editor pane on the right, under "Content Link", add the URL to your txt file and click "Apply" and you're done.

接下来,将 CEWP 添加到您的页面并选择“编辑 Web 部件”。在右侧的内容编辑器窗格中,在“内容链接”下,将 URL 添加到您的 txt 文件,然后单击“应用”即可完成。

Take a look at the following URL for a full description of this change in SharePoint 2010: http://sptwentyten.wordpress.com/2010/08/31/insert-javascript-into-a-content-editor-web-part-cewp/

有关 SharePoint 2010 中此更改的完整说明,请查看以下 URL:http: //sptwentyten.wordpress.com/2010/08/31/insert-javascript-into-a-content-editor-web-part-西普/

回答by James Love

Use jQuery - probably far safer than a document.write which can break javascript further down the page.

使用 jQuery - 可能比 document.write 安全得多,它可以进一步破坏页面下方的 javascript。

Or use the code in this link to put pure HTML in the CEWP instead of dabbling with JavaScript:

或者使用此链接中的代码将纯 HTML 放入 CEWP 中,而不是涉足 JavaScript:

http://karinebosch.wordpress.com/silverlight-meets-sharepoint/walkthrough-2-hosting-silverlight-3-in-a-content-query-web-part/

http://karinebosch.wordpress.com/silverlight-meets-sharepoint/walkthrough-2-hosting-silverlight-3-in-a-content-query-web-part/

回答by Peter Jacoby

Another option is to the HTML Form Web Part(in the Forms category). This can be used to connect to other web parts, but more simply it can used to edit JavaScript directly in the web part. It seems that the rules for Content Editor Web Parts do not apply to the HTML Form Web Parts so it allows more flexibility.

另一个选项是HTML 表单 Web 部件(在表单类别中)。这可用于连接到其他 Web 部件,但更简单地说,它可用于直接在 Web 部件中编辑 JavaScript。内容编辑器 Web 部件的规则似乎不适用于 HTML 表单 Web 部件,因此它提供了更大的灵活性。

More information from Microsoft is here:

来自微软的更多信息在这里:

http://office.microsoft.com/en-us/sharepoint-server-help/use-the-html-form-web-part-to-filter-and-display-data-in-another-web-part-HA101791813.aspx#_Toc274731120

http://office.microsoft.com/en-us/sharepoint-server-help/use-the-html-form-web-part-to-filter-and-display-data-in-another-web-part- HA101791813.aspx#_Toc274731120