简体   繁体   English

如何在onclick事件上重新加载jquery?

[英]How to reload jquery on onclick event?

I've got a table row where it retrieves data from MySQL and I have included a .onclick function where it opens up a text editor with data inside, but the text editor is only opening for the first row and not the rest in the table. 我有一个表行,它从MySQL检索数据,我已经包含一个.onclick函数,它打开一个文本编辑器,里面有数据,但文本编辑器只打开第一行,而不是表中的其余部分。

This is the jQuery code: The first opens the text editor and the second switches the textarea for the text editor which is ckeditor. 这是jQuery代码:第一个打开文本编辑器,第二个切换文本编辑器的textarea是ckeditor。

<script type="text/javascript">

    $(document).ready(function()
    {
        $(".trClass").click(function()
        {
            var id = $(this).attr('id');
            $('#d_'+id).css("display", "block");
            $('#table_id').css("display", "none");
        });
    }); 

    window.onload = function()
    {
        CKEDITOR.replace("editor1");
    };

</script>

and this here is echo for the table, I am using a foreach. 这是表的回声,我正在使用foreach。

echo '
    <tr class="trClass" id="'.$counter.'">
        <td class="marker">
            <i class="fa fa-align-left"></i>
        </td>

        <td class="title">
            '.$article_title.'
        </td>

        <td class="content">
            '.$article_content.'
            </td>
    </tr>

    <section id="d_'.$counter.'" style="display:none;">
        <textarea id="editor1">
            <div style="width:468px;">
                '.$article_content_full.'
            </div>
        </textarea>
    </section>
';
$counter++;
}

I cannot figure out how to make the CKEDITOR.replace("editor1"); 我无法弄清楚如何制作CKEDITOR.replace("editor1"); load for every table, I tried using .click function within it but it does not work as it doesn't load. 为每个表加载,我尝试在其中使用.click函数,但它不起作用,因为它没有加载。 Here is the problem, if you click on the first row it opens the text editor if you click on the second it does not; 这是问题所在,如果你点击第一行它会打开文本编辑器,如果你点击第二行它没有; http://www.goo.gl/dQrLPN http://www.goo.gl/dQrLPN

Typically the id attribute should be unique for each element. 通常, id属性对于每个元素应该是唯一的。 Applying properties across multiple elements is usually accomplished with a class . 跨多个元素应用属性通常通过class来完成。 Knowing this, CKEditor is probably just grabbing the first instance of an object with the given id (probably using document.GetElementById behind the scenes). 知道这一点,CKEditor可能只是抓住具有给定id的对象的第一个实例(可能在幕后使用document.GetElementById )。

(According to the documentation , CKEDITOR.replace(var) will either take a DOM element, ID, or name.) (根据文档CKEDITOR.replace(var)将采用DOM元素,ID或名称。)

Given that, you have a couple of options. 鉴于此,您有几个选择。 One is to defer loading the CKEditor until you actually click on the table row. 一种是推迟加载CKEditor直到你实际点击表格行。 This would look something like this... (note how each textarea has a unique id) 这看起来像这样...(注意每个textarea如何具有唯一的id)

<section id="d_' . $counter . '" style="display:none;">
    <textarea id="editor_'.$counter.'">
        <div style="width:468px;">
            '.$article_content_full.'
        </div>
    </textarea>

$(document).ready(function()
{
    $(".trClass").click(function()
    {
        var id = $(this).attr('id');
        $('#d_'+id).css("display", "block");
        $('#table_id').css("display", "none");
        CKEDITOR.replace('editor_'+id);
    });
}); 

The second option would be to loop through all of the textarea elements and call replace on each one of them on-load. 第二个选项是循环遍历所有textarea元素,并在每个元素上加载调用replace。 I wouldn't really recommend this, unless there's some specific reason you want to load everything up-front. 我不会真的推荐这个,除非有一些特定的原因你想要预先加载所有东西。

EDIT: Although this should fix your issue, you should look in to the HTML issues the other answerers have put forward. 编辑:虽然这应该可以解决您的问题,但您应该查看其他回答者提出的HTML问题。 <section> doesn't belong as a child of <table> :-) <section>不属于<table>的孩子:-)

Try targeting the table and filtering by the rows using the .on() method. 尝试使用.on()方法定位表并按行过滤。

$(document).ready(function()
{
    $('#table_id').on('click','.trClass',function() {
        var id = $(this).attr('id');
        $('#d_'+id).css('display', 'block');
        $('#table_id').css('display', 'none');
    });
});

Details on .on() are here: https://api.jquery.com/on/ 有关.on()详细信息, .on()访问: https.on()

And as noted in the comments above, there are some overall issues with your HTML that you should probably fix before proceeding further. 正如上面的评论中所指出的,在进一步操作之前,您可能应该修复HTML的一些整体问题。

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

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