我正在尝试使用SPServices更新网页内容。
当我运行我的代码时,它会更新正确的页面,但它不会更新内容,而只是删除内容。
var newContent = "<p>This is a test</p>";
$().SPServices({
operation: "UpdateListItems",
listName: "Pages",
ID: itemID,
async: false,
valuepairs: [["PublishingPageContent", newContent]]
})
答案 0 :(得分:0)
这是因为UpdateListItems
操作需要将HTML 转义字符串作为参数。
以下函数可用于编码HTML字符串:
function htmlEncode(value){
//create a in-memory div, set it's inner text(which jQuery automatically encodes)
//then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
已修复示例
var itemID = <Your item id goes here>;
var newContent = "<p>Some content goes here</p>";
$().SPServices({
operation: "UpdateListItems",
listName: "Pages",
ID: itemID,
async: false,
valuepairs: [["PublishingPageContent", htmlEncode(newContent)]],
completefunc: function(xData, Status)
{
console.log('Page has been updated');
}
});