简体   繁体   中英

javascript indexof within jquery element

I have some code here that looks for every mention of "[code]" and turns it into <code> but I only want to do this within a specific jquery element...

      function formattxt(text){
        if (text == '') return '';

        var start = text.indexOf('[code]');
        var end = text.indexOf('[/code]', start);

...

How would I do that?

I'm looking for something simple like the following:

var start = $("#element").text.indexOf('[code]');
var end =  $("#element").text.indexOf('[/code]', start);

阅读jQuery api

$('#element').text().indexOf('[code]');

To Enable the replacement, you'd better use replace :

function replaceTag (text) {
  $('#element').text().replace('[' + text + ']', '<' + text + '>')
}

If you want ot use indexOf , use the following as mentioned by @zzzzBov

$('#element').text().indexOf('[code]'); 

Be careful with indexOf , as it is not supported by IE6-8, You need to include thid code to function in all browsers.

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