jQuery 用jQuery删除变量的第一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6817927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove first char of a variable with jQuery
提问by m4mpfi
I'm trying to modify a jQuery variable from e.g. /images/tree.jpgto images/tree.jpg(without the first slash, so it should be a relative path instead of an absolute).
我正在尝试将 jQuery 变量从例如/images/tree.jpg修改为images/tree.jpg(没有第一个斜杠,所以它应该是相对路径而不是绝对路径)。
I get the URL like this: var href = jQuery("img.thumbnail").attr("href");
我得到这样的 URL: var href = jQuery("img.thumbnail").attr("href");
Now I need this URL on another place, but without the first char.
现在我需要在另一个地方使用这个 URL,但没有第一个字符。
Is there an easy way to do this? Thanks!!
是否有捷径可寻?谢谢!!
回答by Brad Christie
var href = jQuery('img.thumbnail').attr('href'); // e.g. '#foo'
var secondCharacterAndOnward = href.substring(1); // e.g. 'foo'
Though I'm not sure why an image tag has a href
attribute, to be quite honest...
虽然我不确定为什么图像标签有一个href
属性,但老实说......
回答by ShankarSangoli
Use the javascript substring method.
使用 javascript 子字符串方法。
var href = jQuery("img.Thumbnail").attr("href");
href = href.substring(1);