[英]Javascript - How to escape double and single quotes on Kendo template
我在查看頁面(剃刀)上使用kendolistview。 在劍道模板中,我有此代碼。 它只是打開帶有引導程序的彈出窗口。 問題是“內容”數據具有一些“和”值。因此,該鏈接無法引用js函數,並且無法打開彈出窗口。
我嘗試替換數據層中的引號:
Content = rss.CONTENT.Replace(@"\""", "\"").Replace("'", @"\'")
Summary = rss.SUMMARY.Replace(@"\""", "\"").Replace("'", @"\'")
視圖
@model AIS.UI.WebService.Proxy.DSrvAllService.NewsItem
@using Kendo.Mvc.UI
@(Html.Kendo().ListView<AIS.UI.WebService.Proxy.DSrvAllService.NewsItem>()
.Name("listView")
.TagName("div")
.ClientTemplateId("template")
.DataSource(dataSource => dataSource
.Model(model =>model.Id("ID"))
.ServerOperation(true)
.PageSize(2)
.Events(events => events.Error("onError"))
.Read(read => read.Action("GetNewsList", "News"))
)
.Pageable()
)
<script type="text/x-kendo-template" id="template">
....
<a
href="javascript:openNewsPopup('#:Title1#','#:Summary#','#:Content#','#:ImageURL#','#:AddTime#
','#:AddDate#','#:AddYear#','#:SubmitedBy#')" class="btn pull-right"><span style="font-
weight:normal" rel="tooltip" title="Read more about the announcement">Read More</span></a>
<script>
JS(視圖模型)
var OpenNewsPopupViewModel = function() {
var self = this;
self.Title = ko.observable("");
self.Summary = ko.observable("");
self.Content = ko.observable("");
self.ImageURL = ko.observable("");
self.AddTime = ko.observable("");
self.AddDate = ko.observable("");
self.AddYear = ko.observable("");
self.SubmitedBy = ko.observable("");
self.ImageURLFull = ko.observable("");
};
var openNewsPopupViewModel = null;
function openNewsPopup(pTitle, pSummary, pContent, pImageURL, pAddTime, pAddDate, pAddYear, pSubmitedBy) {
var imageRootPath = '@Url.Content("~/Images/Announcements/NewsTypes/NewsType")';
var isNewViewModel = (openNewsPopupViewModel == null);
var newsPopup = $("#newsPopup");
if (isNewViewModel)
{
openNewsPopupViewModel = new OpenNewsPopupViewModel();
var newsPopupTag = newsPopup.get()[0];
ko.applyBindings(openNewsPopupViewModel, newsPopupTag);
}
// pContent = str.replace(/\"/g, "\\\"");
openNewsPopupViewModel.Title(pTitle);
openNewsPopupViewModel.Summary(pSummary);
openNewsPopupViewModel.Content(pContent);
openNewsPopupViewModel.ImageURL(pImageURL);
openNewsPopupViewModel.AddTime(pAddTime);
openNewsPopupViewModel.AddDate(pAddDate);
openNewsPopupViewModel.AddYear(pAddYear);
openNewsPopupViewModel.SubmitedBy(pSubmitedBy);
openNewsPopupViewModel.ImageURLFull("");
openNewsPopupViewModel.ImageURLFull(imageRootPath + '.' + openNewsPopupViewModel.ImageURL() + '.jpg');
newsPopup.modal('show');
}
示例數據(應替換為雙引號)
<a href="javascript:openNewsPopup(' Version 1 is Released','Version 1.0
is now available on web. You can download the new version of the
client program from the website" http:="" 255.255.255.0="" test"="" or=""
installed="" program="" can="" automatically="" download="" it="" for=""
you.','news="" context="" will="" be="" placed="" here.="" news="" .=""
','update','12:04',="" '01.01','2013','admin')"="" class="btn pull-right"><span
style="font-weight:normal" rel="tooltip" title="Read more about the
announcement">Read More</span></a>
您需要先對字符串進行編碼,然后再將其發送給客戶端。 如果在Razor視圖中設置字符串,則只需調用@Html.Encode(string)
。 要在Razor視圖之外(例如,在數據訪問代碼中)對內容進行編碼,只需直接調用System.Web.HttpUtility.HtmlEncode(string)
。
HttpUtility.HtmlEncode("A simple 'encoded' \"string.\"");
// A simple 'encoded' "string."
每個不屬於模板表達式的#符號-#:#,##或#=#-必須轉義\\#。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.