简体   繁体   中英

How can I make a javascript that works in html also work in cshtml?

I have this html page I copied from here . There is a javascript used that changes text areas into rich text editors. It works like it should when I just copy it in a file and open it with a browser. However, when I try to put it in my c# project and put it in a cshtml file (without changing anything) it ignores(?) the javascript and shows the textareas as plain textareas. How can I make this work in cshtml?

The code, same in html page as cshtml page:

<div id="sample">
  <script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> 
  <script type="text/javascript">
      //<![CDATA[
      bkLib.onDomLoaded(function () { nicEditors.allTextAreas() });
      //]]>
  </script>
  <h4>
  First Textarea
  </h4>
  <textarea name="area1" cols="35">
</textarea><br />
  <h4>
    Second Textarea
  </h4>
  <textarea name="area2" style="width: 100%;">
       Some Initial Content was in this textarea
</textarea><br />
  <h4>
    Third Textarea
  </h4>
  <textarea name="area3" style="width: 300px; height: 100px;">
       HTML content default in textarea
</textarea>
</div>

When you copied and pasted the code into your cshtml, did you reference their copy of the nicEdit-latest.js file, or did you save a local copy of it.

As far as I can see it should work if you copied the HTML over exactly the way they have it.

try using the following if you are referencing a local copy of the js file

<script src="@Url.Content("~/PATH_TO_YOUR_FILE/nicEdit-latest.js")" type="text/javascript"></script>

EDIT This should work.

Add a reference Jquery to if you haven't yet, and modify the code to look like this

//<![CDATA[
 $(document).ready(function() {nicEditors.allTextAreas() }) 
//]]>

jsfiddle example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM