Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript]Dynamiczne dodawanie
Forum PHP.pl > Forum > Przedszkole
maki1234
Witam, Piszę skrypty w PHP, toteż w JS jestem zielony jak trawka na wiosnę wink.gif

Tworzę sobie właśnie system BBcode i mam takie widzimisie że po naciśnieciu buttona np [img] w polu tekstowym pojawi się gotowy szablon img (nie mogę napisać bo nie przjmie skrypt dodawania postów, ale chyba każdy wie jak to wygląda)
(identycznie jak przy tworzeniu na tym forum nowego tematu zaznaczając jaka kategoria np PHP to dodaje do tematu tag biggrin.gif. Mniemam że to właśnie w JS można takie coś osiągnąc. Jak już wyżej wspomniałem jestem w JS zielony, są jakieś gotowe rozwiązania? Nakierujecie coś koledzy?

Pozdro!
daniel1302
http://www.dreamincode.net/forums/topic/24...iny-bit-of-css/

Tutaj masz wszystko dokładnie opisane, Kilka osób już uczyło się z tego na praktykach w firmie której pracuję i całkiem nieźle im szło.
maki1234
No ok już coś mam wink.gif Pytanie czy muszę to wszystko przyswoić żeby działało, nie można jakichś gotowców pobrać?
daniel1302
Można cały edytor pobrać
TinyMCE(http://www.tinymce.com/)
CKEdytor(http://ckeditor.com/)

MESSIAH :)
W JS musisz użyć funkcji getElementById i podać nazwę pola do którego chcesz wrzucić swój tag - [img][/img] oczywiście w JS musisz również podać wartość jaką chcesz dodać.
maki1234
Z JS nie wiem za bardzo nic, mógłbyś podać jakiś przykład jeśli wiesz jak to zrobić?

Ogarnąłem takie coś, ale nie działa wie ktoś w czym tkwi problem? (kod nie mój!)

Kod
<!DOCTYPE HTML>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Text editor</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    
    <script type="text/javascript">
        function view_text () {
            // Find html elements.
            var textArea = document.getElementById('my_text');
            var div = document.getElementById('view_text');
            
            // Put the text in a variable so we can manipulate it.
            var text = textArea.value;
            
            // Make sure html and php tags are unusable by disabling < and >.
            text = text.replace(/\</gi, "<");
            text = text.replace(/\>/gi, ">");
            
            // Exchange newlines for <br />
            text = text.replace(/\n/gi, "<br />");
            
            // Basic BBCodes.
            text = text.replace(/\[b\]/gi, "<b>");
            text = text.replace(/\[\/b\]/gi, "</b>");
            
            text = text.replace(/\[i\]/gi, "<i>");
            text = text.replace(/\[\/i\]/gi, "</i>");
            
            text = text.replace(/\[u\]/gi, "<u>");
            text = text.replace(/\[\/u\]/gi, "</u>");
            
            // Print the text in the div made for it.
            div.innerHTML = text;
        }
        
        function mod_selection (val1,val2) {
            // Get the text area
            var textArea = document.getElementById('my_text');
            
            // IE specific code.
            if( -1 != navigator.userAgent.indexOf ("MSIE") ) {
                
                var range = document.selection.createRange();
                var stored_range = range.duplicate();
                
                if(stored_range.length > 0) {
                    stored_range.moveToElementText(textArea);
                    stored_range.setEndPoint('EndToEnd', range);
                    textArea.selectionstart = stored_range.text.length - range.text.length;
                    textArea.selectionend = textArea.selectionstart + range.text.length;
                }
            }
            // Do we even have a selection?
            if (typeof(textArea.selectionstart) != "undefined") {
                // Split the text in three pieces - the selection, and what comes before and after.
                var begin = textArea.value.substr(0, textArea.selectionstart);
                var selection = textArea.value.substr(textArea.selectionstart, textArea.selectionend - textArea.selectionstart);
                var end = textArea.value.substr(textArea.selectionend);
                
                // Insert the tags between the three pieces of text.
                textArea.value = begin + val1 + selection + val2 + end;
            }
        }
    </script>
    
    
</head>
<body>
    
    <!-- Knapper -->
    <input type="button" value="B" onclick="mod_selection('[b]','[/b]')" />
    <input type="button" value="I" onclick="mod_selection('[i]','[/i]')" />
    <input type="button" value="U" onclick="mod_selection('[u]','[/u]')" />
    <br />
    
    <!-- Text area -->
    <textarea class="text_edit" id="my_text"></textarea>
    <br />
    
    <!-- Submit button -->
    <input type="button" value="Show text" onclick="view_text()" />
    
    <!-- Empty div to put the text in -->
    <div id="view_text">
    </div>

</body>
</html>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.