JS中引用HTML的无效或意外令牌

时间:2017-02-24 16:15:42

标签: javascript html sharepoint

代码:

    (function () {

var itemCtx = {};
itemCtx.Templates = {};

itemCtx.Templates.Header = “<div><b>Announcements</b></div><table>”; <---syntax error here?
itemCtx.Templates.Item = ItemOverrideFun;
itemCtx.Templates.Footer = “</table>”; <---syntax error here?

itemCtx.BaseViewID = 1;
itemCtx.ListTemplateType = 104;

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx);

})();

function ItemOverrideFun(ctx) {

var _announcementTitle = ctx.CurrentItem.Title;

var _announcementDesc = ctx.CurrentItem.Body;

var _announcementID = ctx.CurrentItem.ID;

return “<tr><td><p><b>” + _announcementTitle + “</b></p>” + _announcementDesc +”<a href=’/Lists/Company%20Announcements/DispForm.aspx?ID=’+ _announcementID +’> Read More…</a></td></tr>”; <---syntax error here?
} 

调用此代码后,Chrome控制台会显示无效或意外的令牌语法错误。

错误显示在带双引号的行上,在Chrome控制台中,双引号实际上被替换为看起来像带有问号的钻石的图标。

到目前为止,我已经尝试用单引号交换它们并用单引号括起双引号,但两者都没有用。

谢谢。

3 个答案:

答案 0 :(得分:1)

替换为"

正如控制台中显示的错误消息一样,您正在使用的中显然存在错误。您使用的与要使用的"不同。

因此,用替换"的所有出现。

LEFT DOUBLE QUOTATION MARK"QUOTATION MARK。你应该明白这一点  两者都不同。

点击this查看,查看" declare an array of 10 strings arr initialized to empty strings set arr_index to 0 initialize index to first position in string loop find first uppercase letter after index if none find: exit loop if found at new_index copy characters from index (inclusive) to new_index (exclusive) to a new string store that string in arr[arr_index] increment arr_index set index = new_index end loop arr_index is the number of words found

答案 1 :(得分:1)

拉尔提出了一个很好的观点。我已经修好了。

下面:

”<a href=’/Lists/Company%20Announcements/DispForm.aspx?ID=’+ _announcementID +’>

这就是问题:

_announcementID

您曾尝试使用单引号关闭字符串,在连接// using System; // using System.Web.Caching; // https://stackoverflow.com/a/42443437 // Usage: HttpRuntime.Cache.GetOrStore("myKey", () => GetSomethingToCache()); public static class CacheExtensions { private static readonly object sync = new object(); private static TimeSpan defaultExpire = TimeSpan.FromMinutes(20); public static T GetOrStore<T>(this Cache cache, string key, Func<T> generator) => cache.GetOrStore(key, generator, defaultExpire); public static T GetOrStore<T>(this Cache cache, string key, Func<T> generator, TimeSpan expire) { var result = cache[key]; if (result == null) { lock (sync) { result = cache[key]; if (result == null) { result = generator(); cache.Insert(key, result, null, DateTime.Now.AddMinutes(expire.TotalMinutes), Cache.NoSlidingExpiration); } } } return (T)result; } }

之前需要双倍才能将其关闭

答案 2 :(得分:1)

您需要使用正确的字符替换所有单引号和双引号,然后使用双引号正确引用链接的其他片段:_announcementID+"'> Read More...</a>

(function () {

var itemCtx = {};
itemCtx.Templates = {};

itemCtx.Templates.Header = "<div><b>Announcements</b></div><table>";
itemCtx.Templates.Item = ItemOverrideFun;
itemCtx.Templates.Footer = "</table>";

itemCtx.BaseViewID = 1;
itemCtx.ListTemplateType = 104;

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx);

})();

function ItemOverrideFun(ctx) {

var _announcementTitle = ctx.CurrentItem.Title;

var _announcementDesc = ctx.CurrentItem.Body;

var _announcementID = ctx.CurrentItem.ID;

return "<tr><td><p><b>" + _announcementTitle + "</b></p>" + _announcementDesc +"<a href='/Lists/Company%20Announcements/DispForm.aspx?ID="+ _announcementID+"'> Read More...</a></td></tr>";
}