我很难理解为什么变量在以下代码中无法正常工作。
var welcomeMsg = $('#accountbar span.welcome_text').text(); // This returns "Welcome, Nick(account) "
var cutdown = welcomeMsg.slice(0, -6); // This removes excessive spaces after (account)
cutdown = cutdown.slice(cutdown.indexOf(",")+2,cutdown.indexOf("(")); // returns "Nick"
var username = cutdown; // "Nick" //This doesn't work with last line of the script
//alert(username);
//var username = "Nick" // but this works if it's used instead of var username = cutdown;
//alert(username);
$("td:contains('"+username+"')").parent().css({
"background" : "url('http://imagehost.com/forum/row1.png')"
});
这是一个 Greasemonkey 脚本,如果这很重要的话。
已解决:
这似乎.trim
是删除所需字符串周围过多空格的更好方法。如果这个脚本非常适合我
var cutdown = welcomeMsg.slice(0, -6); // This removes excessive spaces after (account)
cutdown = cutdown.slice(cutdown.indexOf(",")+2,cutdown.indexOf("("));
替换为这个
var cutdown = welcomeMsg.slice(welcomeMsg.indexOf(",")+2,welcomeMsg.indexOf("("));
cutdown = cutdown.trim();