JQuery - “Селектор” для текущей позиции в документе

Мне действительно нравится помещать маленький оператор на ту же строку как если

int myFunc(int x) {
   if(x >20) return -1;
   //do other stuff ....
}
5
задан marc_s 9 May 2016 в 08:56
поделиться

3 ответа

I do not know a solution using jQuery, but you can find out the DOM node in the current caret position in most browsers quite easily using:

  • document.selection.createRange() in IE (see MSDN)
  • window.getSelection().focusNode in most other browsers (Gecko et al, see MDC Article)

Good luck!

0
ответ дан 15 December 2019 в 01:06
поделиться

Если я правильно понимаю, вам нужна функция, работающая точно так же, как document.write, я не понимаю, почему вам это нужно, а не просто использовать document.write?

1
ответ дан 15 December 2019 в 01:06
поделиться

это просто: просто дайте атрибут id вашему встроенному тегу скрипта:

  <script id="hello">alert("hello");</script>

, тогда вы можете просто сделать:

$('script#hello').after('<span style="color:red">this is my new content</span>');

ОБНОВЛЕНИЕ:

, поскольку вы не хотите изменять html, то единственной альтернативой является доступ к тегам сценария в соответствии с их порядком появления в коде.

  $('script').each(function(index){
// "index" gives you the instance number, which you could use, for example inside a switch case.
switch(index){
case '1':
    $(this).after('<span style="color:red">i am number 1</span>');
break;

case '2':
    $(this).after('<span style="color:red">i am number 2</span>');
break;

default:
    $(this).after('<span style="color:red">i am number '+index+'</span>');
break;
}
    });

или вы можете получить доступ к конкретному тегу через .eq (index):

$('script').eq(2).after('hello world');  
// this is for the third script tag, starting from the top of the document.
0
ответ дан 15 December 2019 в 01:06
поделиться
Другие вопросы по тегам:

Похожие вопросы: