javascript SyntaxError:无法在“文档”上执行“querySelector”:“[object HTMLDocument]”不是有效的选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22577783/
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-10-27 23:24:35  来源:igfitidea点击:

SyntaxError: Failed to execute 'querySelector' on 'Document': '[object HTMLDocument]' is not a valid selector

javascriptjquerysharepointsharepoint-2010csom

提问by Luis Valencia

I am trying to get all field values from a sharepoint list item on a display template, the ctx.CurrentItem only gets some values but not all I need.

我试图从显示模板上的共享点列表项中获取所有字段值,ctx.CurrentItem 只获取一些值,但不是我需要的全部。

I tried the following code, but I get the error on the title

我尝试了以下代码,但在标题上出现错误

SyntaxError: Failed to execute 'querySelector' on 'Document': '[object HTMLDocument]' is not a valid selector.

语法错误:无法在“文档”上执行“querySelector”:“[object HTMLDocument]”不是有效的选择器。

function GetListItemById_Success(sender, args) 
{ 
   var id = ListItem.get_id(); 
   var title = ListItem.get_item("Title"); 
   alert("Updated List Item: \n Id: " + id + " \n Title: " + title); 
}

// Display an appropriate error message 
function GetListItemById_Fail(sender, args) 
{ 
   alert("GetListItemById Failed. \n" + args.get_message() + "\n" + args.get_stackTrace());
}

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(CustomAction, "sp.js"); });

function CustomAction(){
var clientContext =  new SP.ClientContext.get_current();
var web = clientContext .get_web(); 
var targetList = clientContext.get_web().get_lists().getByTitle('Test Document Library');
var list = web.get_lists().getByTitle('Test Document Library'); 
ListItem = list.getItemById(ctx.CurrentItem.DocId);
clientContext.load(ListItem, "Id", "Title"); 
clientContext.executeQueryAsync(GetListItemById_Success, GetListItemById_Fail); 
}

采纳答案by vstrale

I got the same exception after upgrading jQuery located in top level site. The reason was reference to old jQuery library in subsitemaster page.

升级位于顶级站点的 jQuery 后,我遇到了同样的异常。原因是在站点母版页中引用了旧的 jQuery 库。