Diuji dan sekarang berfungsi (versi asli tidak mengulangi semua elemen .comment-body
, atau menemukan substring()
dengan benar):
var divString, imgString;
$('.comment-body').each(
function(){
divString = $(this).text();
imgString = divString.substring(divString.indexOf('[img]') + 5,divString.indexOf('[/img]'));
console.log(imgString);
});
JS Fiddle.
Diedit karena saya sedikit bosan, sehingga mengubah fungsi di atas menjadi fungsi yang lebih umum:
function findStringBetween(elem,bbTagStart,bbTagClose){
var tag = bbTagStart;
function impliedEndTag(tag){
var impliedEnd = tag.replace(tag.substring(0,1),tag.substring(0,1) + '/');
return impliedEnd;
}
var endTag = bbTagClose || impliedEndTag(tag);
var divString = $(elem).text();
var tagString = divString.substring(divString.indexOf('[img]') + tag.length,divString.indexOf('[/img'));
return tagString;
}
$('.comment-body').each(
function(){
/* call with two, or three arguments (the third is the optional 'bbTagClose':
1. elem = this, the DOM node,
2. '[img]' = whatever bbCode thing you want to use (I'm not sure what's available),
3. 'bbTagClose' = the end tag of the bbCode, assuming that the end-tag is the same as
the opening tag, except with a '/' as the second character, the impliedEndTag() function
will take care of it for you.
*/
var elemString = findStringBetween(this,'[img]');
$(this).replaceWith('<img src="' + elemString + '" class="commentimg" data-src2="'+ elemString +'"/>');
});
Demo JS Fiddle.
Diedit mengikuti pertanyaan lebih lanjut dari OP (dalam komentar, di bawah):
...fungsinya menambahkan '' ke setiap div dengan badan komentar kelas bagaimana saya hanya bisa menerapkan kode ke elemen badan komentar yang berisi [img]gambar src di sini[/img]
Saya telah menambahkan beberapa pemeriksaan kewarasan, untuk memastikan bahwa fungsi mengembalikan false ketika tag yang ditentukan tidak ditemukan:
function findStringBetween(elem,bbTagStart,bbTagClose){
var tag = bbTagStart;
function impliedEndTag(tag){
var impliedEnd = tag.replace(tag.substring(0,1),tag.substring(0,1) + '/');
return impliedEnd;
}
var endTag = bbTagClose || impliedEndTag(tag);
var divString = $(elem).text().trim(); // added .trim() to remove white-spaces
if (divString.indexOf(tag) != -1){ // makes sure that the tag is within the string
var tagString = divString.substring(divString.indexOf('[img]') + tag.length,divString.indexOf('[/img'));
return tagString;
}
else { // if the tag variable is not within the string the function returns false
return false;
}
}
$('.comment-body').each(
function(){
/* call with two, or three arguments (the third is the optional 'bbTagClose':
1. elem = this, the DOM node,
2. '[img]' = whatever bbCode thing you want to use (I'm not sure what's available),
3. 'bbTagClose' = the end tag of the bbCode, assuming that the end-tag is the same as
the opening tag, except with a '/' as the second character, the impliedEndTag() function
will take care of it for you.
*/
var imgLink = findStringBetween(this,'[img]');
if (imgLink){ // only if a value is set to the variable imgLink will the following occur
$(this).replaceWith('<img src="' + imgLink + '" class="commentimg" data-src2="'+ imgLink+'"/>');
}
});
Demo JS Fiddle.
Diedit sebagai jawaban atas pertanyaan lebih lanjut dari OP (dalam komentar, di bawah):
[Apakah] ada cara untuk mencegahnya menghapus teks dalam contoh 'teks acak di sini'[?]
Ya, Anda dapat .append()
, atau .prepend()
gambar ke dalam elemen, setelah terlebih dahulu memperbarui teks div
, dalam kode berikut saya telah menghapus string [img]...[/img]
, membiarkan hanya teks lainnya, disisipkan teks itu ke dalam elemen .comment-body
dan kemudian menambahkan img
ke dalamnya, alih-alih menggunakan replaceWith()
:
function findStringBetween(elem,bbTagStart,bbTagClose){
var tag = bbTagStart;
function impliedEndTag(tag){
var impliedEnd = tag.replace(tag.substring(0,1),tag.substring(0,1) + '/');
return impliedEnd;
}
var endTag = bbTagClose || impliedEndTag(tag);
var divString = $(elem).text().trim();
if (divString.indexOf(tag) != -1){
var elemInfo = [];
elemInfo.imgString = divString.substring(divString.indexOf(tag) + tag.length,divString.indexOf(endTag));
elemInfo.text = divString.replace(tag + elemInfo.imgString + endTag, '');
return elemInfo;
}
else {
return false;
}
}
$('.comment-body').each(
function(){
/* call with two, or three arguments (the third is the optional 'bbTagClose':
1. elem = this, the DOM node,
2. '[img]' = whatever bbCode thing you want to use (I'm not sure what's available),
3. 'bbTagClose' = the end tag of the bbCode, assuming that the end-tag is the same as
the opening tag, except with a '/' as the second character, the impliedEndTag() function
will take care of it for you.
*/
var elemInfo = findStringBetween(this,'[img]');
if (elemInfo.imgString){
// or .prepend() if you prefer
$(this).text(elemInfo.text).append('<img src="' + elemInfo.imgString + '" class="commentimg" data-src2="'+ elemInfo.imgString +'"/>');
}
});
Demo JS Fiddle.
Referensi:
person
David says reinstate Monica
schedule
04.12.2011
div
. Apakah ini yang ingin Anda capai atau hanya sebuah kesalahan? - person kubetz   schedule 05.12.2011