הוסטס - פורום אחסון האתרים הגדול בישראל

הוסטס - פורום אחסון האתרים הגדול בישראל (https://hosts.co.il/forums/index.php)
-   פורום תיכנות (https://hosts.co.il/forums/forumdisplay.php?f=14)
-   -   [JS] שאלה.. (https://hosts.co.il/forums/showthread.php?t=28285)

amirs_5 07-08-06 19:33

[JS] שאלה..
 
איך מחליפים מילים ב JS
הכוונה זה שאם יש לי את המילה "אבגדה"
ואני רוצה להחליף במילה הזאתי את "אב" ל "אאאא"
אז הפלט יהיה "אאאאגדה"

תודה לעוזרים:)

עריכה :
יש לי עוד שאלה
כתבתי את הדבר הזא

HTML קוד:

function text2color(text,c){
myform.text.value = "<font color="+c+">"+text+"</font>";
}

HTML קוד:

<form action='index.php' method='post' name='myform'>
<input type='button' name="color" onclick="text2color(myform.text.value,'red');" />
<textarea cols='80' rows='10' name='text'>
טקסט
</textarea>
</form>

עכשיו שאני לוחץ על הכפתור בטקסטארא רשום לי
HTML קוד:

<font color=red>טקסט</font>
השאלה שלי היא אני רוצה נגיד לעשות סימון לטקסט ,
שאני יסמן את מהמילה טקסט רק את סט
וכאשר אני ילחץ על הכפתור הפלא יהיה:

HTML קוד:

טק<font color=red>סט</font>

Unknown 07-08-06 20:48

בקשר לשאלה הראשונה:
קוד:

xxx.value.replace(REPLACEWHAT,REPLACE WITH);
בקשר לשניה:
קצת ארוך מקווה שתבין הכל:

קוד:

function is_ie() {
        if (navigator.appName == "Microsoft Internet Explorer")
                return true;
        else
                return false;
}
function text2color(text,c) {
        txtElement = document.forms.myform.text;
        if (is_ie()) {
                txtElement.focus();
                docSelection = document.selection;
                txtSelection = docSelection.createRange();
                if (txtSelection.text != "") {
                        txtSelection.text = '<font color="'+c+'">'+txtSelection.text+'</font>';
                } else {
                        txtElement.value = '<font color="'+c+'">'+txtElement.value+'</font>';
                }
        } else {
                txtElement.focus();
                docSelection = txtElement.value.substring(txtElement.selectionStart, txtElement.selectionEnd);
                if (docSelection != "") {
                        var newValue = "";
                        newValue += txtElement.value.substring(0, txtElement.selectionStart);
                        newValue += '<font color="'+c+'">'+docSelection+'</font>';
                        newValue += txtElement.value.substring(txtElement.selectionEnd, txtElement.value.length);
                        txtElement.value = newValue;
                } else {
                        txtElement.value = '<font color="'+c+'">'+txtElement.value+'</font>';
                }
        }
}


amirs_5 07-08-06 21:03

תודה:)
 
הראשון עזר לי השני גם עובד אבל לא ממש הבנתי..
הבנתי חצי , את הקטע של האחרי הבדיקה של הדפדפן לא הבנתי.. את זה:
HTML קוד:

                txtElement.focus();
                docSelection = document.selection;
                txtSelection = docSelection.createRange();
                if (txtSelection.text != "") {
                        txtSelection.text = '<font color="'+c+'">'+txtSelection.text+'</font>';
                } else {
                        txtElement.value = '<font color="'+c+'">'+text+'</font>';
                }

מישהו יכול להסביר לי את זה ..?
תודה :)

Unknown 07-08-06 21:07

זה מתמקד על הTEXTAREA שלך
ואם זה IE אז זה מקבל ישר את מה שבחרת
אחרי זה, זה הופך את הבחירה למעין RANGE, כלומר זה מאפשר לך לערוך את הבחירה בנפרד
זה בודק אם הבחירה ריקה, זה מוסיף לכל הטקסט בTEXTAREA את הFONT
אם לא, אז זה עורך את הבחירה עצמה

ד"א ערכתי את ההודעה כי הייתה לי טעות בהתחלה אז תתקן את הקוד

amirs_5 07-08-06 21:42

הא תודה תיקנתי:)
ויש לי עוד כמה דברים שלא הבנתי.. :S
למה צריך לבדוק אם זה IE ?
מה עושה focus(); ? אני מכיר onfocus

RS324 07-08-06 21:58

focus ממקד אותך על איזור מסויים בדף
זה יכול להיות DIV INPUT TEXTAREA או כל דבר שתרצה

amirs_5 08-08-06 00:57

הא ובלי הפוקוס שום דבר לא היה עובד כי יש אלמנטים שלא מוגדרים והפוקוס מגדיר אותם לטקסטארא אם הבנתי נכון???

Unknown 08-08-06 01:49

הFOCUS כדי שהבחירה תישאר ולא תיעלם בזמן הלחיצה על הכפתור
צריך לבדוק אם זה IE משום שהדרך להשיג את התוכן המסומן בIE לא תומכת בשל FF / MOZILLA או להיפך

amirs_5 08-08-06 02:31

בקשר לפוקוס, אני הורדתי את השורה הזאתי וזה עובד לי כמו שצריך יכול להיות ?!

Unknown 08-08-06 05:13

כן, זה עניין של נוחות כדי שהסמן ישאר על התיבה, אבל בחירה שלך אם אתה רוצה להשתמש בזה או לא


כל הזמנים הם GMT +2. הזמן כעת הוא 05:04.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ