简体   繁体   English

向Javascript函数添加功能

[英]Add functionality to Javascript function

I am trying to add a dropdown selection and code button to the Wordpress editor in Javascript. 我正在尝试向Javascript中的Wordpress编辑器添加一个下拉选择和代码按钮。

I found this site that has it done already I am just trying to modify the code slightly. 我发现这个网站已经完成了,我只是想稍微修改一下代码。 You can see what I am talking about here, you can select a language from the dropdown and then click the code button on that site. 您可以在这里看到我在说什么,可以从下拉菜单中选择一种语言,然后单击该站点上的代码按钮。

http://bililite.com/blog/blogfiles/customeditor.html 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>';
  });
});

This code currently insert code like this is no language is selected from the dropdwon... 目前,此代码插入的代码是从下拉菜单中选择的没有语言的代码...

<code></code>

And this when a Language is seleected... 当选择一种语言时...

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

All I need to do is wrap this in <pre> the above code here </pre> tags. 我需要做的就是将其包装在<pre> the above code here </pre>标记中。 I can easily add in the opening pre tag in the code above but I am unable to add the closing tag, I could really use some help from someone who knows Javascript better 我可以轻松地在上面的代码中添加开始标记,但是我无法添加结束标记,我确实可以使用一些了解Java脚本的人的帮助

You should be able to add the closing tags pretty similar to the starting tags. 您应该能够添加与开始标签非常相似的结束标签。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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