Я пытаюсь закрыть тег с помощью javascript, но когда он записывает в документ, косая черта всегда отсутствует. Я пробовал перед ним обратную косую черту («\ /»), но, похоже, это помогает. Я продолжаю видеть <pre>
на источнике страницы. Вот код:закрытие тега с помощью javascript
var temp_index = 0,
line_count = 0;
while (temp_index < text.length) {
if ((text.charAt(temp_index) == "\n") && (line != line_count)) {
if (line_count < line) line_count++;
if (line_count == line) {
text = text.substring(0, temp_index) + "<pre id='line'>" + text.substring(temp_index);
temp_index++;
while ((text.charAt(temp_index) != "\n") && (temp_index < text.length)) temp_index++;
text = text.substring(0, temp_index - 1) + "<\pre>" + text.substring(temp_index);
}
}
temp_index++;
}
return text;
Я ожидал получить:
Heres's the last line
<pre id='line'>Here's the current line</pre>
Here's the next line
Here's the final line
Но я получаю:
Here's the last line
<pre id='line'>Here's the current line
Here's the next line
Here's the final line</pre>
Я сделал быстро исправить, заменив \ п на конец строки с тегом. Несмотря на то, что проблема устранена, она вызывает ошибки при вводе клавиатуры. Вот обновленный код.
if (line_count == line) {
text = text.substring(0, temp_index) + "<pre id=\"line\">" + text.substring(temp_index);
temp_index++;
while ((text.charAt(temp_index) != "\n") && (temp_index < text.length)) temp_index++;
text = text.substring(0, temp_index - 1) + "</pre>" + text.substring(temp_index);
break;
}
попытался с помощью '// '? –
Да, я просто сделал, я все еще вижу < pre> – tay10r
Вам не нужно скрывать косую черту, поэтому «//» фактически равно «//». Обратные косые черты - другая история, поэтому, если у вас есть «\\» в строке, это приведет к «\». – doublesharp