javascript Jquery,检查 charAt() 是否等于 unicode 字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4384150/
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
Jquery, check if charAt() is equal to a unicode character?
提问by SventoryMang
I've got a collapsible link which has the unicode arrow on it
我有一个可折叠的链接,上面有 unicode 箭头
?This is a collapsible link
And when someone clicks on it I want it to be turned into the down arrow ▼. However I am having trouble figuring out how to parse to replace this character.
当有人点击它时,我希望它变成向下箭头 ▼。但是,我无法弄清楚如何解析以替换此字符。
Here is my code:
这是我的代码:
function CollapsibleContainerTitleClickApp(){
$(".collapsibleContainerContent.app", $(this).parent()).slideToggle();
alert($(this).text().trim().charAt(0));
if ($(this).text.trim().charAt(0) == "\u25B8"{
alert("inside the if statement");
$(this).text($(this).text().replace("\u25B8", "\u25BE"));
}else{
$(this).text($(this).text().replace("\u25BE", "\u25B8"));
}
Now the first alert always pops up as the actual arrow (?) and viewing the source also has the actual arrow. How can I see if the first character is one arrow, and if so replace it with the other arrow? The second alert statement never fires so it's never passing the condition of the if.
现在第一个警报总是作为实际箭头 (?) 弹出,查看源也有实际箭头。如何查看第一个字符是否是一个箭头,如果是,则将其替换为另一个箭头?第二个警报语句永远不会触发,因此它永远不会通过 if 的条件。
回答by Jacob Relkin
You have multiple errors in your ifstatement, this is why that branch is never hit.
您的if语句中有多个错误,这就是该分支永远不会被命中的原因。
if ($(this).text.trim().charAt(0) == "\u25B8"{
You forgot the closing )of the ifstatement.
你忘记)了if声明的结尾。
Also, you're not invoking the text()method, you're trying to access it as a member, which it isn't.
此外,您不是在调用该text()方法,而是试图以成员身份访问它,而事实并非如此。
Your code should look like this:
您的代码应如下所示:
if ($(this).text().trim().charAt(0) == "\u25B8") {
回答by fragmentedreality
You can solve this with css and jQuery, too:
你也可以用 css 和 jQuery 来解决这个问题:
CSS:
CSS:
.collapsible_link:before { content: "BC"; }
.collapsed_link:before { content: "BA"; }
jQuery:
jQuery:
$('#link').click(function(o) {
$(o).toggleClass('collapsible_link collapsed_link');
});
回答by Lee
charCodeAt(index)returns the integer unicode value of the character at the specified index. This is often the easiest way to test individual char values -- especially when they're non-printable, or special in some other way. (docs at w3schools)
charCodeAt(index)返回指定索引处字符的整数 unicode 值。这通常是测试单个字符值的最简单方法——尤其是当它们不可打印或以其他方式特殊时。 (w3schools 的文档)
Also... be sure you're comparing the correct unicode values. "\u25B8"is the "small right arrow" (?) -- but the arrow you showed in your example is the "large right arrow", which is "\u25BA"(?)
另外...请确保您正在比较正确的 unicode 值。 "\u25B8"是“小右箭头” ( ?) -- 但您在示例中显示的箭头是“大右箭头”,即"\u25BA"( ?)
so... a simple example using charCodeAt()andthe correct unicode value:
所以......一个简单的例子使用charCodeAt()和正确的unicode值:
var s = "?This is a collapsible link";
if(s.charCodeAt(0) === 0x25BA) {
alert( "true!");
}

