2012-06-11 5 views
-1

jquery를 사용하여 textarea에 값을 넣으 려합니다. 다음 코드를 사용하고 있습니다. 그것은 작동하지 않습니다.JavaScript를 사용하여 JQuery WYSIWYG 편집기의 내용을 변경하십시오.

<link rel="stylesheet" href="css/jquery.wysiwyg.css" type="text/css" /> 
    <script type="text/javascript" src="js/jquery.wysiwyg.js"></script> 
    <script type="text/javascript"> 
    function wedit(topic){ 
    $("#wysiwyg").html(topic); 
    alert(id+topic+instructor+date+video); 
    } 
</script> 
<form name="theform" action="" method="post"> 
<table border="0" cellpadding="10" cellspacing="10" width="680px"> 
<tr><td width="150">Topic:</td><td><span id="wtopic"> 
    <textarea name="wysiwyg" id="wysiwyg" ></textarea></span></td></tr> 
    <tr><td></td><td><input type="submit" name="submit" value="Save" 
    title="Save" /></td> </tr> 
    </table> 
</form> 

텍스트의 경우 작동하지만 텍스트 영역의 경우 빈 값을 표시합니다. jquery 또는 javascript를 통해 textarea에 텍스트를 넣을 수있는 방법을 알려주십시오.

+7

문제를 재현 할 수있는 가장 간단한 샘플로 코드를 줄이십시오. 우리가 그런 간단한 문제를 해결하기에는 너무 많은 코드입니다. – meagar

+0

순식간에이 대답으로 인해 범프가 15 초 만에 쉽게 발견됩니다. 당신은 분명히 원시 unminified 형태의 코드는 300 라인 미만에서 당신이 원할 수있는 모든 대답을 가지고있는 JS 파일을 가지고 있습니다. 거기에 JQuery.WYSIWYG 파일과 함께 여러 장소에 링크 된 위키 페이지도이 개념을 설명합니다. – VulgarBinary

답변

0

읽고 자바 스크립트에서/그것을 쓰기 때, 텍스트 영역에 나타나는에 <textarea></textarea> 태그 결과 사이에 물건을 넣어 텍스트 영역을 만들 때, 당신은 .value="content" 속성을 사용하거나 jQuery를의 경우에는 필요하지만 , .val("content"). 나는 $("#wysiwyg").html(topic);를 볼 수 있지만, 그것은 아마해야합니다 :

$("#wysiwyg").val(topic); 
+0

두 기능을 모두 시도했지만 작동하지 않았습니다. – user1429132

+0

실제로'wedit' 함수를 실행하고 있습니까? riiighhttt? –

+0

예. 버튼에서 wedit 함수 onclick을 호출합니다. – user1429132

1

http://code.google.com/p/jwysiwyg/wiki/Introduction

우선, 위키는 모든 것을 가지고 당신이 필요합니다. 확실하지 귀하의 질문은

.... 그 buuuuuuuuuuuuut 과거 무엇을 얻기 내용 할 수 있습니다 단지 JQuery와에서

$('#wysiwyg').val(); 

내용 조작 하는 경우를() 메소드를 .val 사용하여 텍스트 영역의 콘텐츠를 당신은 다음 방법을 사용할 수있는 jwysiwyg textarea 내용을 조작하고 싶습니다

이미지 삽입 텍스트에 이미지 삽입

$('#wysiwyg').wysiwyg('insertImage', 'http://domain.com/image.png'); 

이 질문에 대한 대답을 찾지 못한 경우 가 선택한 텍스트

$('#wysiwyg').wysiwyg('createLink', 'http://google.com/'); 

를 통해 링크를 삽입 링크를 삽입, 난 프로토 타입 스파이크 jquery.wysiwyg 어딘가에 여기 주위에 누워 알고 .js가 포함되어 있습니다. 당신은 당신이 (.wysiwyg 그렇게하기 전에 수행해야 HTML를 삽입하려면 ... 여기-선제 사전 그러나이 될 것

편집는 원시 텍스트 상자처럼 초기화 후 마음 jwysiwyg 더 이상 기능을 유지 .. .) init 호출. MVC @ Url.Content (...) 메소드를 무시하십시오. 아래에서 필요한 것을 얻을 수 있어야합니다. 당신은 당신이 지금 컨트롤 자체가 사용자에게 노출 된 API를 통해 작동 할 수있는 컨트롤을 초기화하기 후 모든 JQuery와 UI 등을 염두에

<link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/jwysiwyg/jquery.wysiwyg.css")" /> 
<script type="text/javascript" src="@Url.Content("~/Content/jwysiwyg/jquery.wysiwyg.js")"></script> 
<script type="text/javascript" src="@Url.Content("~/Content/jwysiwyg/controls/wysiwyg.link.js")"></script> 
<script type="text/javascript" src="@Url.Content("~/Content/jwysiwyg/controls/wysiwyg.table.js")"></script> 

<script type="text/javascript"> 
    $(function(){ 
     $('#wysiwyg').wysiwyg({ 
      initialContent: "**INITIAL CONTENT HERE IF YOU WANT TO INJECT SOMETHING AT INIT TIME**", 
      css: "@Url.Content("~/Content/comments.css")?upd=00000002", 
      controls: { 
       bold: { visible: true }, 
       italic: { visible: true }, 
       underline: { visible: true }, 
       strikeThrough: { visible: true }, 

       insertImage: { visible: false }, 

       justifyLeft: { visible: true }, 
       justifyCenter: { visible: true }, 
       justifyRight: { visible: true }, 
       justifyFull: { visible: true }, 

       indent: { visible: true }, 
       outdent: { visible: true }, 

       subscript: { visible: true }, 
       superscript: { visible: true }, 

       undo: { visible: true }, 
       redo: { visible: true }, 

       insertOrderedList: { visible: true }, 
       insertUnorderedList: { visible: true }, 
       insertHorizontalRule: { visible: true }, 
       cut: { visible: true }, 
       copy: { visible: true }, 
       paste: { visible: true }, 
       html: { visible: false }, 
       increaseFontSize: { visible: true }, 
       decreaseFontSize: { visible: true }, 

       h1: { visible: false }, 
       h2: { visible: false }, 
       h3: { visible: false }, 
       exam_html: { visible: false } 
      }, 
      events: { 
       click: function (event) { 

       } 
      } 
     }); 
    }); 
</script> 

<textarea rows="10" cols="100" id="wysiwyg" name="@Model.FormName"> 
    <!-- HTML GOES HERE --> 
</textarea> 

편집 # 2

지키는 제어합니다. 이 경우 $ ("# YourTextArea")입니다. wysiwyg ("method", "arguments");

JQuery에서 수행 할 수있는 전체 작업 목록.WYSIWYG 컨트롤은 도구 모음 이름과 일치하는 JS 파일에서 쉽게 찾을 수 있지만 원하는 경우 (목록을 정리하지 않고 정리할 필요가없는 경우 혼란 스럽지만 한 단계 씩 들여 쓰기를 읽어야 함) 메소드 이름)를 수집

bold: { 
      groupIndex: 0, 
      visible: true, 
      tags: ["b", "strong"], 
      css: { 
       fontWeight: "bold" 
      }, 
      tooltip: "Bold", 
      hotkey: { "ctrl": 1, "key": 66 } 
     }, 

     copy: { 
      groupIndex: 8, 
      visible: false, 
      tooltip: "Copy" 
     }, 

     createLink: { 
      groupIndex: 6, 
      visible: true, 
      exec: function() { 
       var self = this; 
       if ($.wysiwyg.controls && $.wysiwyg.controls.link) { 
        $.wysiwyg.controls.link.init(this); 
       } else if ($.wysiwyg.autoload) { 
        $.wysiwyg.autoload.control("wysiwyg.link.js", function() { 
         self.controls.createLink.exec.apply(self); 
        }); 
       } else { 
        console.error("$.wysiwyg.controls.link not defined. You need to include wysiwyg.link.js file"); 
       } 
      }, 
      tags: ["a"], 
      tooltip: "Create link" 
     }, 

     unLink: { 
      groupIndex: 6, 
      visible: true, 
      exec: function() { 
       this.editorDoc.execCommand("unlink", false, null); 
      }, 
      tooltip: "Remove link" 
     }, 

     cut: { 
      groupIndex: 8, 
      visible: false, 
      tooltip: "Cut" 
     }, 

     decreaseFontSize: { 
      groupIndex: 9, 
      visible: false, 
      tags: ["small"], 
      tooltip: "Decrease font size", 
      exec: function() { 
       this.decreaseFontSize(); 
      } 
     }, 

     h1: { 
      groupIndex: 7, 
      visible: true, 
      className: "h1", 
      command: ($.browser.msie || $.browser.safari) ? "FormatBlock" : "heading", 
      "arguments": ($.browser.msie || $.browser.safari) ? "<h1>" : "h1", 
      tags: ["h1"], 
      tooltip: "Header 1" 
     }, 

     h2: { 
      groupIndex: 7, 
      visible: true, 
      className: "h2", 
      command: ($.browser.msie || $.browser.safari) ? "FormatBlock" : "heading", 
      "arguments": ($.browser.msie || $.browser.safari) ? "<h2>" : "h2", 
      tags: ["h2"], 
      tooltip: "Header 2" 
     }, 

     h3: { 
      groupIndex: 7, 
      visible: true, 
      className: "h3", 
      command: ($.browser.msie || $.browser.safari) ? "FormatBlock" : "heading", 
      "arguments": ($.browser.msie || $.browser.safari) ? "<h3>" : "h3", 
      tags: ["h3"], 
      tooltip: "Header 3" 
     }, 

     highlight: { 
      tooltip: "Highlight", 
      className: "highlight", 
      groupIndex: 1, 
      visible: false, 
      css: { 
       backgroundColor: "rgb(255, 255, 102)" 
      }, 
      exec: function() { 
       var command, node, selection, args; 

       if ($.browser.msie || $.browser.safari) { 
        command = "backcolor"; 
       } else { 
        command = "hilitecolor"; 
       } 

       if ($.browser.msie) { 
        node = this.getInternalRange().parentElement(); 
       } else { 
        selection = this.getInternalSelection(); 
        node = selection.extentNode || selection.focusNode; 

        while (node.style === undefined) { 
         node = node.parentNode; 
         if (node.tagName && node.tagName.toLowerCase() === "body") { 
          return; 
         } 
        } 
       } 

       if (node.style.backgroundColor === "rgb(255, 255, 102)" || 
         node.style.backgroundColor === "#ffff66") { 
        args = "#ffffff"; 
       } else { 
        args = "#ffff66"; 
       } 

       this.editorDoc.execCommand(command, false, args); 
      } 
     }, 

     html: { 
      groupIndex: 10, 
      visible: false, 
      exec: function() { 
       var elementHeight; 

       if (this.options.resizeOptions && $.fn.resizable) { 
        elementHeight = this.element.height(); 
       } 

       if (this.viewHTML) { //textarea is shown 
        this.setContent(this.original.value); 

        $(this.original).hide(); 
        this.editor.show(); 

        if (this.options.resizeOptions && $.fn.resizable) { 
         // if element.height still the same after frame was shown 
         if (elementHeight === this.element.height()) { 
          this.element.height(elementHeight + this.editor.height()); 
         } 

         this.element.resizable($.extend(true, { 
          alsoResize: this.editor 
         }, this.options.resizeOptions)); 
        } 

        this.ui.toolbar.find("li").each(function() { 
         var li = $(this); 

         if (li.hasClass("html")) { 
          li.removeClass("active"); 
         } else { 
          li.removeClass('disabled'); 
         } 
        }); 
       } else { //wysiwyg is shown 
        this.saveContent(); 

        $(this.original).css({ 
         width: this.element.outerWidth() - 6, 
         height: this.element.height() - this.ui.toolbar.height() - 6, 
         resize: "none" 
        }).show(); 
        this.editor.hide(); 

        if (this.options.resizeOptions && $.fn.resizable) { 
         // if element.height still the same after frame was hidden 
         if (elementHeight === this.element.height()) { 
          this.element.height(this.ui.toolbar.height()); 
         } 

         this.element.resizable("destroy"); 
        } 

        this.ui.toolbar.find("li").each(function() { 
         var li = $(this); 

         if (li.hasClass("html")) { 
          li.addClass("active"); 
         } else { 
          if (false === li.hasClass("fullscreen")) { 
           li.removeClass("active").addClass('disabled'); 
          } 
         } 
        }); 
       } 

       this.viewHTML = !(this.viewHTML); 
      }, 
      tooltip: "View source code" 
     }, 

     increaseFontSize: { 
      groupIndex: 9, 
      visible: false, 
      tags: ["big"], 
      tooltip: "Increase font size", 
      exec: function() { 
       this.increaseFontSize(); 
      } 
     }, 

     indent: { 
      groupIndex: 2, 
      visible: true, 
      tooltip: "Indent" 
     }, 

     insertHorizontalRule: { 
      groupIndex: 6, 
      visible: true, 
      tags: ["hr"], 
      tooltip: "Insert Horizontal Rule" 
     }, 

     insertImage: { 
      groupIndex: 6, 
      visible: true, 
      exec: function() { 
       var self = this; 

       if ($.wysiwyg.controls && $.wysiwyg.controls.image) { 
        $.wysiwyg.controls.image.init(this); 
       } else if ($.wysiwyg.autoload) { 
        $.wysiwyg.autoload.control("wysiwyg.image.js", function() { 
         self.controls.insertImage.exec.apply(self); 
        }); 
       } else { 
        console.error("$.wysiwyg.controls.image not defined. You need to include wysiwyg.image.js file"); 
       } 
      }, 
      tags: ["img"], 
      tooltip: "Insert image" 
     }, 

     insertOrderedList: { 
      groupIndex: 5, 
      visible: true, 
      tags: ["ol"], 
      tooltip: "Insert Ordered List" 
     }, 

     insertTable: { 
      groupIndex: 6, 
      visible: true, 
      exec: function() { 
       var self = this; 

       if ($.wysiwyg.controls && $.wysiwyg.controls.table) { 
        $.wysiwyg.controls.table(this); 
       } else if ($.wysiwyg.autoload) { 
        $.wysiwyg.autoload.control("wysiwyg.table.js", function() { 
         self.controls.insertTable.exec.apply(self); 
        }); 
       } else { 
        console.error("$.wysiwyg.controls.table not defined. You need to include wysiwyg.table.js file"); 
       } 
      }, 
      tags: ["table"], 
      tooltip: "Insert table" 
     }, 

     insertUnorderedList: { 
      groupIndex: 5, 
      visible: true, 
      tags: ["ul"], 
      tooltip: "Insert Unordered List" 
     }, 

     italic: { 
      groupIndex: 0, 
      visible: true, 
      tags: ["i", "em"], 
      css: { 
       fontStyle: "italic" 
      }, 
      tooltip: "Italic", 
      hotkey: { "ctrl": 1, "key": 73 } 
     }, 

     justifyCenter: { 
      groupIndex: 1, 
      visible: true, 
      tags: ["center"], 
      css: { 
       textAlign: "center" 
      }, 
      tooltip: "Justify Center" 
     }, 

     justifyFull: { 
      groupIndex: 1, 
      visible: true, 
      css: { 
       textAlign: "justify" 
      }, 
      tooltip: "Justify Full" 
     }, 

     justifyLeft: { 
      visible: true, 
      groupIndex: 1, 
      css: { 
       textAlign: "left" 
      }, 
      tooltip: "Justify Left" 
     }, 

     justifyRight: { 
      groupIndex: 1, 
      visible: true, 
      css: { 
       textAlign: "right" 
      }, 
      tooltip: "Justify Right" 
     }, 

     ltr: { 
      groupIndex: 10, 
      visible: false, 
      exec: function() { 
       var p = this.dom.getElement("p"); 

       if (!p) { 
        return false; 
       } 

       $(p).attr("dir", "ltr"); 
       return true; 
      }, 
      tooltip: "Left to Right" 
     }, 

     outdent: { 
      groupIndex: 2, 
      visible: true, 
      tooltip: "Outdent" 
     }, 

     paragraph: { 
      groupIndex: 7, 
      visible: false, 
      className: "paragraph", 
      command: "FormatBlock", 
      "arguments": ($.browser.msie || $.browser.safari) ? "<p>" : "p", 
      tags: ["p"], 
      tooltip: "Paragraph" 
     }, 

     paste: { 
      groupIndex: 8, 
      visible: false, 
      tooltip: "Paste" 
     }, 

     redo: { 
      groupIndex: 4, 
      visible: true, 
      tooltip: "Redo" 
     }, 

     removeFormat: { 
      groupIndex: 10, 
      visible: true, 
      exec: function() { 
       this.removeFormat(); 
      }, 
      tooltip: "Remove formatting" 
     }, 

     rtl: { 
      groupIndex: 10, 
      visible: false, 
      exec: function() { 
       var p = this.dom.getElement("p"); 

       if (!p) { 
        return false; 
       } 

       $(p).attr("dir", "rtl"); 
       return true; 
      }, 
      tooltip: "Right to Left" 
     }, 

     strikeThrough: { 
      groupIndex: 0, 
      visible: true, 
      tags: ["s", "strike"], 
      css: { 
       textDecoration: "line-through" 
      }, 
      tooltip: "Strike-through" 
     }, 

     subscript: { 
      groupIndex: 3, 
      visible: true, 
      tags: ["sub"], 
      tooltip: "Subscript" 
     }, 

     superscript: { 
      groupIndex: 3, 
      visible: true, 
      tags: ["sup"], 
      tooltip: "Superscript" 
     }, 

     underline: { 
      groupIndex: 0, 
      visible: true, 
      tags: ["u"], 
      css: { 
       textDecoration: "underline" 
      }, 
      tooltip: "Underline", 
      hotkey: { "ctrl": 1, "key": 85 } 
     }, 

     undo: { 
      groupIndex: 4, 
      visible: true, 
      tooltip: "Undo" 
     }, 

     code: { 
      visible: true, 
      groupIndex: 6, 
      tooltip: "Code snippet", 
      exec: function() { 
       var range = this.getInternalRange(), 
        common = $(range.commonAncestorContainer), 
        $nodeName = range.commonAncestorContainer.nodeName.toLowerCase(); 
       if (common.parent("code").length) { 
        common.unwrap(); 
       } else { 
        if ($nodeName !== "body") { 
         common.wrap("<code/>"); 
        } 
       } 
      } 
     }, 

     cssWrap: { 
      visible: false, 
      groupIndex: 6, 
      tooltip: "CSS Wrapper", 
      exec: function() { 
       $.wysiwyg.controls.cssWrap.init(this); 
      } 
     } 

이제 아무것도 내가 그래서 당신은 항상 할 수있다이 경우 ;-)

를 제공 할 수 없습니다 :. $ ("#의 YourTextArea") WYSIWYG ("단락", " 귀하의 텍스트 "); 또는 툴바의 다른 방법. 경고 : HTML은 여러분이 생각하는 것이 아니기 때문에 미리 지적 해주십시오.

+0

jqeury가 텍스트를 텍스트 영역에 넣으 려합니다. – user1429132

+0

내 편집을 참조하십시오. 텍스트 영역을 더 이상 원시 텍스트 영역이 아닌 RTE 컨트롤을 사용하고 있습니다. 초기화 후 내용을 삽입하려면 J.WYSIWYG API를 사용해야합니다. 초기화하는 동안 정보를 전달할 수있는 "초기 내용"인수가 있지만 일단 초기화되면 API를 사용해야합니다. 이 경우에는 – VulgarBinary

+0

편집기가 작동합니다. 텍스트를 텍스트 영역에 삽입하고 싶습니다. – user1429132

관련 문제