简体   繁体   English

Twitter喜欢jQuery中的文本

[英]Twitter Like text in jquery

I am trying to walk through a table and get a specific TD with a name of note and cut it off at 40 characters and display "..." if the text is over 40 characters. 我试图遍历一张桌子,得到一个带有注释名称的特定TD,并将其切成40个字符,如果文本超过40个字符,则显示“ ...”。

I wrote: 我写:

var element = $('.maintenanceTable').find('td[name="note"]');
console.log(element.text().length);
if(element.text().length > 40){
    element.text().substring(0, 10) + '....';
}
console.log(element)

The first console.log shows 115 characters, the second, shows a object returned .... What am I doing wrong? 第一个console.log显示115个字符,第二个console.log显示返回的对象....我在做什么错?

jQuery functions like "text" generally have two forms. 诸如“文本”之类的jQuery函数通常具有两种形式。 If you supply no arguments to them (eg text() ), they give you the value contained in the element. 如果您不提供任何参数(例如text() ),则它们会为您提供元素中包含的值。 But if you pass an argument (eg text("something") ), then it sets the value. 但是,如果您传递参数(例如text("something") ),则它将设置该值。

So you want something like this: 所以你想要这样的东西:

var element = $('.maintenanceTable').find('td[name="note"]').first();
if(element.text().length > 40) {
  element.text(
    element.text().substring(0, 39) + '...'
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM