簡體   English   中英

向Javascript函數添加功能

[英]Add functionality to Javascript function

我正在嘗試向Javascript中的Wordpress編輯器添加一個下拉選擇和代碼按鈕。

我發現這個網站已經完成了,我只是想稍微修改一下代碼。 您可以在這里看到我在說什么,可以從下拉菜單中選擇一種語言,然后單擊該站點上的代碼按鈕。

http://bililite.com/blog/blogfiles/customeditor.html

jQuery(function ($) {
  edButtons.forEach(function (button, index) {
    if (button.display == 'code') {

      // add language pull down menu
      edButtons[index + 1] = { // insert right after the code button
        html: function (idPrefix) {
          return '<select id="' + idPrefix + 'code_language" data-code-target="' + index + '">' + '<option></option>' + // include a blank option
          '<option>' + languages.join('</option><option>') + '</option>' + '</select>';
        }
      };
    }
  });

  var languages = ['html', 'javascript', 'lisp', 'pdf', 'php', 'vb'];
  $('body').on('change', 'select[data-code-target]', function () {
    var lang = $(this).val();
   // edButtons[$(this).data('code-target')].tagStart = lang ? '<code class="language-' + lang + '" >' : '<code>';
    edButtons[$(this).data('code-target')].tagStart = lang ? '<pre><code data-language="' + lang + '">' : '<pre><code>';
  });
});

目前,此代碼插入的代碼是從下拉菜單中選擇的沒有語言的代碼...

<code></code>

當選擇一種語言時...

<code data-language="php"></code>

我需要做的就是將其包裝在<pre> the above code here </pre>標記中。 我可以輕松地在上面的代碼中添加開始標記,但是我無法添加結束標記,我確實可以使用一些了解Java腳本的人的幫助

您應該能夠添加與開始標簽非常相似的結束標簽。 嘗試這個:

edButtons[$(this).data('code-target')].tagEnd = lang ? '</pre></code>' : '</code>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM