Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将2个ASCII字符与javascript进行比较_Javascript_Jquery_Html - Fatal编程技术网

将2个ASCII字符与javascript进行比较

将2个ASCII字符与javascript进行比较,javascript,jquery,html,Javascript,Jquery,Html,我有一个部门如下: <div class="job-article-holder-click-for-more-link"> &#x25B2; </div> $('.job-article-holder-click-for-more-link').on('click', function () { $(this).html() == "&#x25B2;" ? $(this).html('&#x25BC;') : $(this).

我有一个部门如下:

<div class="job-article-holder-click-for-more-link">
    &#x25B2;
 </div>
$('.job-article-holder-click-for-more-link').on('click', function () {
    $(this).html() == "&#x25B2;" ? $(this).html('&#x25BC;') : $(this).html('&#x25B2;');
});

每次检查都失败,为什么?它基本上是一个箭头,我只是尝试在单击它时上下切换它。

可能html返回的内容中有空格。尝试修剪html()输出


与检查实际HTML相比,获取实际字符并使用检查其ascii(实际上是unicode)值可能更安全。如前所述,您需要首先修剪内容:

$('.job-article-holder-click-for-more-link').on('click', function () {
    $(this).text().trim().charCodeAt(0) == 9650 ?
        $(this).html('&#x25BC;') : $(this).html('&#x25B2;');
});

首先,在比较之前,您需要删除大量空白:
$.trim($('this.html())==“▲”
,第二个
html()
可以返回也可以不返回字符实体代码(“code>▲)或字符本身。如果您赢了,您已经在我之前几秒钟发布了答案:-)
$('.job-article-holder-click-for-more-link').on('click', function () {
    $(this).text().trim().charCodeAt(0) == 9650 ?
        $(this).html('&#x25BC;') : $(this).html('&#x25B2;');
});