ทดสอบแล้วใช้งานได้แล้ว (เวอร์ชันดั้งเดิมไม่ได้วนซ้ำผ่านองค์ประกอบ .comment-body
ทั้งหมด หรือค้นหา substring()
อย่างถูกต้อง):
var divString, imgString;
$('.comment-body').each(
function(){
divString = $(this).text();
imgString = divString.substring(divString.indexOf('[img]') + 5,divString.indexOf('[/img]'));
console.log(imgString);
});
JS ซอ
แก้ไข เนื่องจากฉันรู้สึกเบื่อนิดหน่อย จึงเปลี่ยนฟังก์ชันข้างต้นให้เป็นฟังก์ชันทั่วไปมากขึ้น:
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 +'"/>');
});
การสาธิต JS Fiddle
แก้ไข ตามคำถามเพิ่มเติมจาก OP (ในความคิดเห็นด้านล่าง):
...ฟังก์ชั่นจะเพิ่ม '' ให้กับทุกๆ div ด้วย class comment-body ฉันจะใช้โค้ดกับองค์ประกอบ comment-body ที่มี [img]image src ที่นี่[/img] ได้อย่างไร
ฉันได้เพิ่มการตรวจสอบสติสองสามอย่างเพื่อให้แน่ใจว่าฟังก์ชันส่งคืนเท็จเมื่อไม่พบแท็กที่กำหนด:
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+'"/>');
}
});
การสาธิต JS Fiddle
แก้ไข เพื่อตอบคำถามเพิ่มเติมจาก OP (ในความคิดเห็นด้านล่าง):
[มี] มีวิธีป้องกันไม่ให้ลบข้อความในตัวอย่างนี้ 'ข้อความสุ่มที่นี่'[?]
ใช่ คุณสามารถ .append()
หรือ .prepend()
รูปภาพลงในองค์ประกอบได้ หลังจากอัปเดตข้อความของ div
เป็นครั้งแรก ในโค้ดต่อไปนี้ ฉันได้ลบสตริง [img]...[/img]
ออก เพื่อปล่อยให้ เพียง ข้อความอื่นแทรกอยู่ ข้อความนั้นในองค์ประกอบ .comment-body
จากนั้นจึงต่อท้าย img
แทนการใช้ 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 +'"/>');
}
});
การสาธิต JS Fiddle
อ้างอิง:
person
David says reinstate Monica
schedule
04.12.2011
div
ออกแล้ว นี่คือสิ่งที่คุณต้องการบรรลุผลสำเร็จหรือเป็นเพียงความผิดพลาด? - person kubetz   schedule 05.12.2011