简体   繁体   中英

Concatenating strings in JavaScript / jQuery?

I need to remove nested <b></b> tags. See the generated css and you'll understand the problem:

<div class="highlight" contenteditable="true">
    ahuasdhuasdhuasdhu 
    <b>
       <b>#</b>
       huadshuashud
    </b>
</div>

I want remove the tags that have a '# inside, but I don't want remove the '#'. (Note the hashtag can be any another value). For this, I wrote the following code:

// Highlight already been declared and just is a simple $('.highlight').
highlight.children('b').children('b').contents().unwrap();

Now I have the following code:

<div class="highlight" contenteditable="true">
    ahuasdhuasdhuasdhu 
    <b>
        "#"
        "huadshuashud"
    </b>
</div>

I want join this two strings, because when I double click it, it just select "#" or "huadshuashud", but I want that all content inside the tag is selected.

Have some way to improve my method or concatenate these two strings?

Try:

$('.highlight > b').html(function(){
  return $(this).text();
});

Which will give you this:

<b>
  #
  huadshuashud
</b>

Selecting text in an element (akin to highlighting with your mouse)

Here you have a ready function. Edit the beginning to be able to pass an object instead of id and add a handler:

$(".highlight").ondblclick(function(){
    SelectText(this)
})

function SelectText(elementObject) {
    // here choose function you like and replace text variable
    text = elementObject;
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM