替换unicode字符

时间:2017-06-07 14:33:33

标签: javascript html unicode replace

我无法弄清楚如何使用.replace(...)删除Unicode字符,这是我尝试过的

 $(elem).click(function () {
   var display = $("." + target).css('display');
   var lastChar = display == 'none' ? 'a' : '8';
   $("." + target).slideToggle(500);
   alert('index: ' + $(this).text().indexOf('⇈'));
   $(this).html($(this).html().replace('⇈','').replace('⇊','') + ' &#x21c' + lastChar + ';');
 });

它正在向上和向下添加我的双箭头,但indexOf始终为-1,而我的替换调用不会删除Unicode字符。我现在正在考虑这个问题并且认为如果我从这些Unicode字符中的一个开始,我可以用另一个替换它...如果我可以替换它来工作; - )

我做错了什么?谢谢!

2 个答案:

答案 0 :(得分:1)

HTML实体转换为实际的unicode字符。例如:



/**
* Accordion-folding functionality.
*
* Markup with the appropriate classes will be automatically hidden,
* with one section opening at a time when its title is clicked.
* Use the following markup structure for accordion behavior:
*
* <div class="accordion-container">
*   <div class="accordion-section open">
*       <h3 class="accordion-section-title"></h3>
*       <div class="accordion-section-content">
*       </div>
*   </div>
*   <div class="accordion-section">
*       <h3 class="accordion-section-title"></h3>
*       <div class="accordion-section-content">
*       </div>
*   </div>
*   <div class="accordion-section">
*       <h3 class="accordion-section-title"></h3>
*       <div class="accordion-section-content">
*       </div>
*   </div>
* </div>
*
* Note that any appropriate tags may be used, as long as the above classes are present.
*
* @since 3.6.0.
*/

( function( $ ){

$( document ).ready( function () {

    // Expand/Collapse accordion sections on click.
    $( '.accordion-container' ).on( 'click keydown', '.accordion-section-title', function( e ) {
        if ( e.type === 'keydown' && 13 !== e.which ) { // "return" key
            return;
        }

        e.preventDefault(); // Keep this AFTER the key filter above

        accordionSwitch( $( this ) );
    });

});

/**
 * Close the current accordion section and open a new one.
 *
 * @param {Object} el Title element of the accordion section to toggle.
 * @since 3.6.0
 */
function accordionSwitch ( el ) {


    var section = el.closest( '.accordion-section' ),
        sectionToggleControl = section.find( '[aria-expanded]' ).first(),
        container = section.closest( '.accordion-container' ),
        siblings = container.find( '.open' ),
        siblingsToggleControl = siblings.find( '[aria-expanded]' ).first(),
        content = section.find( '.accordion-section-content' );

    // This section has no content and cannot be expanded.
    if ( section.hasClass( 'cannot-expand' ) ) {
        return;
    }





    // Add a class to the container to let us know something is happening inside.
    // This helps in cases such as hiding a scrollbar while animations are executing.
    container.addClass( 'opening' );

    if ( section.hasClass( 'open' ) ) {
        section.toggleClass( 'open' );
        content.toggle( true ).slideToggle( 150 );
    } else {
        siblingsToggleControl.attr( 'aria-expanded', 'false' );
        siblings.removeClass( 'open' );
        siblings.find( '.accordion-section-content' ).show().slideUp( 150 );
        content.toggle( false ).slideToggle( 150 );
        section.toggleClass( 'open' );
    }

    // We have to wait for the animations to finish
    setTimeout(function(){
        container.removeClass( 'opening' );
    }, 150);

    // If there's an element with an aria-expanded attribute, assume it's a toggle control and toggle the aria-expanded value.
    if ( sectionToggleControl ) {
        sectionToggleControl.attr( 'aria-expanded', String( sectionToggleControl.attr( 'aria-expanded' ) === 'false' ) );
    }
}
&#13;
&#13;
&#13;

因此,当您通过document.body.innerHTML = "&#x21c8;"; console.log(document.body.innerHTML === "\u21c8"); // true // Instead of a unicode esape sequence, you can write the // actual unicode character. This is safe as long as you // specify the correct encoding for your JavaScript files: console.log(document.body.innerHTML === "⇈"); // true innerHTML通过$(this).html()阅读textContent时,您需要查找由其unicode转义序列{$(this).text()给出的实际unicode字符。 1}}或直接"\u21c8"而不是其实体"⇈"

答案 1 :(得分:1)

尝试使用String.fromCharCode()

  

静态String.fromCharCode()方法返回由其创建的字符串   使用指定的Unicode值序列。

<强>语法

String.fromCharCode(num1[, ...[, numN]]);

<强>实施例

myString.replace(String.fromCharCode(8648),'');表示⇈ myString.replace(String.fromCharCode(8650),'');代表⇊ myString.indexOf(String.fromCharCode(84,69,83,84);