У меня есть веб-страница с текстом и шорткодами. Я хотел бы заменить шорткоды на ссылки:
[a href="http://www.myurl.com" title="A Link Title"]My link text[/a] and even here [a href="www.yoururl.com" title="Another link" class="linkClass"]This is cool to[/a] !
К этому
<a href="http://www.myurl.com" title="A Link Title">My link text</a> and even here <a href="www.yoururl.com" title="Another link" class="linkClass">This is cool to</a> !
Не могли бы вы помочь мне сделать это в JQuery или Javascript?
Спасибо за помощь.
-3
Te-Deum
28 Окт 2019 в 17:45
2 ответа
Лучший ответ
Наконец-то я нашел способ сделать это с помощью этого сценария: https://github.com/nicinabox/shortcode.js
Вот мой код для обработки моего случая:
new Shortcode(document.querySelector('body'), {
a: function(done) {
var ret='<a';
if (typeof this.options.href !== 'undefined' && this.options.href !== null) ret+=' href="'+this.options.href+'"';
if (typeof this.options.class !== 'undefined' && this.options.class !== null) ret+=' class="'+this.options.class+'"';
if (typeof this.options.title !== 'undefined' && this.options.title !== null) ret+=' title="'+this.options.title+'"';
if (typeof this.options.style !== 'undefined' && this.options.style !== null) ret+=' style="'+this.options.title+'"';
if (typeof this.options.target !== 'undefined' && this.options.target !== null) ret+=' target="'+this.options.title+'"';
ret+='>'+this.contents+'</a>';
return ret;
}
});
0
Te-Deum
28 Окт 2019 в 18:26
Попробуйте использовать replace . Надеюсь, это поможет.
function sayHello() {
var str = '[a href="http://www.myurl.com" title="A Link Title"]My link text[/a] and even here [a href="www.yoururl.com" title="Another link" class="linkClass"]This is cool to[/a] !';
while(str.includes("[")){
str = str.replace("[", "<");
}
while(str.includes("]")){
str = str.replace("]", ">");
}
console.log(str);
}
sayHello();
0
kazinayem2011
28 Окт 2019 в 15:42
Похожие вопросы
Новые вопросы
javascript
По вопросам программирования на ECMAScript (JavaScript / JS) и его различных диалектах / реализациях (кроме ActionScript). Включите все соответствующие теги в свой вопрос; например, [node.js], [jquery], [json] и т. д.