javascript - 我的 javascript 中的字符串如果包含 ' 就会被 chop

标签 javascript string

我有一个 javascript 代码,它构造了一个 HTML 以在浏览器中呈现,这是我要询问的 javascript 部分:-

if (bodyValue && bodyValue.length >= 150) 
    { 
        var displayUrl = "/Lists/Feedback/DispForm.aspx?ID="+ctx.CurrentItem.ID+"&Source=https://***/Lists/UserFeedbackSystem/AllItems.aspx"

        newBodyValue = bodyValue.substring(0, 150) + "<a target='_blank' href='" + displayUrl + "'>...[Read More]</a>"; 
    } 
return "<span title='" + bodyValue + "'>" + newBodyValue + "</span>";

现在如果bodyValue包含此字符 '那么 Span 中将只显示字符串的一部分title属性(property)。 例如我有这个 bodyValue :-

​1. Issue started back in 3 weeks and continued recurring until last week.

2. All the resolutions suggest that the issue is with the OS rather than a scanner issue- she's the only user facing this issue.

但是title将显示工具提示的属性将仅显示字符串的这一部分(直到到达 ' ):-

1. Issue started back in 3 weeks and continued recurring until last week.

2. All the resolutions suggest that the issue is with the OS rather than a scanner issue- she

有人可以建议我如何解决这个问题吗?还有哪些其他字符会导致此问题?

编辑 现在我已经更新了我的代码如下,创建一个 <span>并获取其 outerHTML ,如下:-

function bodyFiledTemplate1(ctx) { 

    var bodyValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name]; 
    var regex = /(<([^>]+)>)/ig; 

    bodyValue = bodyValue.replace(regex, "");
    const span = document.createElement('span');
     span.title = bodyValue;
    span.textContent = bodyValue.slice(0, 150);


    var newBodyValue = bodyValue; 

    if (bodyValue && bodyValue.length >= 150) 
    { 
    var displayUrl = "/Lists/Feedback/DispForm.aspx?ID="+ctx.CurrentItem.ID+"&Source=/Lists/UserFeedbackSystem/AllItems.aspx"
       const a = span.appendChild(document.createElement('a'));
     a.href = displayUrl;
     a.target = '_blank';
     a.textContent  = '...[Read More]';
    } 


    return span.outerHTML; 

但是如果我有以下字符串 7:28在我的里面bodyValue ,它将被渲染为 7&#58;28 ..我没有测试其他 Angular 色...

最佳答案

与处理转义问题相比,直接显式分配给每个元素的属性可能更容易管理,而不是尝试将字符串连接在一起以创建 HTML 标记(这既不优雅,又容易出错,并且可能不安全)。例如,创建 span使用document.createElement ,并分配给它的title属性:

const span = document.createElement('span');
span.title = bodyValue;
span.textContent = bodyValue.slice(0, 150);
if (bodyValue.length >= 150) {
  const a = span.appendChild(document.createElement('a'));
  a.href = displayUrl;
  a.target = '_blank';
  a.textContent  = '...[Read More]';
}
return span;

然后您可以附加返回的 <span>到您想要将其附加到的任何容器 appendChild (或类似的东西):

const span = createSpan();
container.appendChild(span);

function createSpan() {
  const bodyValue = `Lorem : ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`;
 const displayUrl = 'https://example.com/';

  const span = document.createElement('span');
  span.title = bodyValue;
  span.textContent = bodyValue.slice(0, 150);
  if (bodyValue.length >= 150) {
    const a = span.appendChild(document.createElement('a'));
    a.href = displayUrl;
    a.target = '_blank';
    a.textContent  = '...[Read More]';
  }
  return span;
}

const span = createSpan();
document.body.appendChild(span);

关于javascript - 我的 javascript 中的字符串如果包含 ' 就会被 chop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55152721/

相关文章:

c++ - 在二叉搜索树中初始化字符串数组

javascript - 动态背景图像不显示在引导模式上

javascript - 如何将多个项目传递给 Handlebars 中的一个函数?

javascript - 如何使用 React Native WebView 组件在我的 Android 应用程序中显示网页的特定部分?

java - 为什么我的应用程序在我单击某个按钮时崩溃?

php - 当一个字符串的开头与另一个不同时?

javascript - 该选择器在超时时间内不起作用

javascript - meteor 集合插入允许抛出错误

在 C 中初始化未知值字符串的正确方法

java - 字符串是第二个的子串?