简体   繁体   English

jquery元素中的javascript indexof

[英]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... 我这里有一些代码可以查找对[[code]]的所有提及,并将其转换为<code>但是我只想在特定的jquery元素内执行此操作...

      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 : 要启用替换,最好使用replace

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

If you want ot use indexOf , use the following as mentioned by @zzzzBov 如果要使用indexOf ,请使用@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. 请小心 indexOf ,因为IE6-8不支持它,您需要包括thid代码才能在所有浏览器中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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