我的脚本有问题,但我自己看不到问题所在:
$("#mainsub").append(newRow + "<div class='showMore' onclick='document.dispatchEvent(expand, { 'postId': " + pf.id + " })'>Show More</div></div>");
我的 webkit 调试器显示:“未捕获的语法错误:意外的标记;”当我点击该元素时。
有人遇到过这种情况吗?这行代码有问题吗?
编辑:** newRow 看起来像这样:
var newRow = "<div id=" + pf.id + ">"+rowContents;
行内容是:
rowContents += "<div class='"+tagName+"'>"+tag.text()+"</div>";
标签名称只是来 self 正在解析的一些 xml。
另一个编辑:我只是将整个函数放入以防万一这有帮助
pf.parseResults = function(){
$("#mainsub").empty();
var $xml = $(pf.xml);
var $query = $xml.find('query_result').children().each(function() {
var row = $(this);
var rowContents = "";
row.children().each(function() {
var tag = $(this);
var tagName = tag[0].tagName;
if(tagName != "ID"){
rowContents += "<div class='"+tagName+"'>"+tag.text()+"</div>";
}
if(tagName === "ID"){
pf.id = tag.text();
}
});
var newRow = "<div id=" + pf.id + ">" + rowContents;
$("#mainsub").append(newRow + "<div class='showMore' onclick='document.dispatchEvent(expand, { \'postId\': " + pf.id + " })'>Show More</div></div>");// this fires an event with data attached listing the id of the element the user tapped/**/
});
$("#mainsub").append("<div onclick='document.dispatchEvent(nextPage)'><p>Next</p></div>");// figure out how to not show next when there will not be another page
if(pf.getPage > 0){
$("#mainsub").append("<div onclick='document.dispatchEvent(previousPage)'><p>Previous</p></div>");// show go to previous as long at it will exist
}
};
最佳答案
当您嵌套相同类型的引号(在您的情况下是单引号)时,您必须像这样转义它们:
$("#mainsub").append(newRow + "<div class='showMore' onclick='document.dispatchEvent(expand, { \'postId\': " + pf.id + " })'>Show More</div></div>");
由于这行代码相当长,而且我不想更改太多,因此我会指出转义引号位于此 expand, {\'postId\': "+ pf.id + "}
片段。
关于javascript - 如何使用jquery将包含数据的dispatchEvent附加到类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23686359/