简体   繁体   English

jQuery修剪功能在IE7中不起作用?

[英]jQuery trim function does not work in IE7?

I'm trying to call the jQuery text() function and run it through the trim() function to remove all trailing and leading whitespace. 我正在尝试调用jQuery text()函数并通过trim()函数运行它以删除所有尾随和前导空格。 Seems to work great in Firefox, however, does not work in IE7 (refuses to remove a space trailing at the end). 似乎在Firefox中工作得很好,然而,在IE7中不起作用(拒绝删除末尾的空格)。

Any ideas?! 有任何想法吗?! Maybe a regex solution? 也许正则表达式解决方案?

you most probably have forgotten about jquery chainning ... 你很可能已经忘记了jquery chainning ......

try this 试试这个

$('#selector').trim($('#selector').text())

don't be lazy with 不要偷懒

$('#selector').text().trim();//this is wrong...

EDIT 编辑

or as @Laserson has simplified it even better, with $.trim($(selector).text()); 或者@Laserson使用$.trim($(selector).text());其简化得更好$.trim($(selector).text());

So here's the gist of what was going on. 所以这里是正在发生的事情的要点。 I had some text in a span element and after that text there was a hyperlink/image the user could click to remove the text from the line it was on (see code below). 我在span元素中有一些文本,在该文本之后有一个超链接/图像,用户可以单击该图像以从其所在的行中删除文本(请参阅下面的代码)。 However, I had put a   但是,我已经放了一个  after the span element text (in the hyperlink's text) to put a bit of padding between the span text and the "delete" image. 在span元素文本之后(在超链接的文本中)在span文本和“delete”图像之间放置一些填充。 So even though I was accessing the element's text and trimming $.trim($(this).parent().text()); 所以即使我正在访问元素的文本并修剪$.trim($(this).parent().text()); it would still include that space at the end! 最后还会包括那个空间! Once I removed this extra space, things worked fine. 一旦我删除了这个额外的空间,事情就好了。 Still no idea why $.trim() wouldn't take care of it though?! 仍然不知道为什么$.trim()不会照顾它呢?!

<div>
  <span>
    <strong>SomeText</strong>
  </span>
  <a href="javascript:void(0);" onclick="removeMe();">&nbsp;
    <img src="delete.png" width="15" height="15" border="0" name="imgRemove" />
  </a>
</div>

Yes i have an idea. 是的我有个主意。 The whitespace at the end is not in the element you are calling your text() function on. 最后的空格不在您调用text()函数的元素中。 This can happen in IE because it treats whitespace differently then firefox, and will give it its own elements, where as firefox will not. 这可能发生在IE中,因为它以不同于firefox的方式处理空白,并且会给它自己的元素,而firefox则不会。 This is just a hunch, because your question doesnt give much to go on. 这只是一种预感,因为你的问题并没有给人留下太多的帮助。

jquery uses /^\\s+|\\s+/g in its trim method- jquery在其trim方法中使用/ ^ \\ s + | \\ s + / g-

Any trailing space would have to be added after it returns. 返回后必须添加任何尾随空格。

It may depend on when you read it- try to alert the value directly from the trim operation. 这可能取决于您何时阅读它 - 尝试直接从修剪操作中提醒值。 If that reads correctly, the space is being added by whatever you do next to the string. 如果读取正确,则通过在字符串旁边执行的操作添加空间。

If trim really returns with a trailing space, use text.replace( /^\\s+|\\s+/g,''). 如果trim真的以尾随空格返回,请使用text.replace(/ ^ \\ s + | \\ s + / g,'')。

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

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