简体   繁体   English

jQuery在另一个on事件中插入元素

[英]Jquery Insert element in an other on event

First post here and quite new on javascript/jquery/webstuff but even after some research about the thing I'm trying to do, I didn't manage to find something that made this thing work. 首先在这里发布,并且在javascript / jquery / webstuff上还很新,但是即使在对我要做的事情进行了一些研究之后,我也没有设法找到使该事情起作用的东西。

So, here is the context : 因此,这是上下文:
I'm creating a schedule with HTML and Javascript/Jquery. 我正在使用HTML和Javascript / Jquery创建时间表。

I want to be able to inject an automated generated <span> or something directly by clicking on the day (which is a <td> ). 我希望能够通过单击当天(这是<td> )直接注入自动生成的<span>或其他内容。

At first I used basic stuff like "getElementById()" but if I manage to do the thing with this, I'm going on a lot of repetitive code. 最初,我使用了诸如“ getElementById()”之类的基本东西,但是如果我设法做到这一点,我将进行大量重复的代码。 And I'm quite sure I can avoid this. 而且我很确定我可以避免这种情况。

So basically I wanted to do generic code and be able to get the which the user clicked. 所以基本上我想做通用代码并能够获得用户点击的代码。

Here is what I managed to do by now: HTML part : 这是我现在设法完成的操作:HTML part:

<table>
    <tr>
        <th class="day-name">Sun</th>
        <th class="day-name">Mon</th>
        <th class="day-name">Tue</th>
        <th class="day-name">Wed</th>
        <th class="day-name">Thu</th>
        <th class="day-name">Fri</th>
        <th class="day-name">Sat</th>
    </tr>

    {% set counter = namespace(a=0) %}
    {% for x in range(0, 5) %}
        <tr class="week">
            {% for y in range(0, 7) %}
                {% set counter.a = counter.a + 1 %}
                <!-- <td id="2" class="day" onclick="addEvent()"><span class="number">{{ counter.a }}</span></td> -->
                <td id="day_{{ counter.a }}" class="day"><span class="number">{{ counter.a }}</span></td>
            {% endfor %}
    {% endfor %}
</table>

Javascript Part : Javascript部分:

function addEvent(){
    var node = document.createElement("span");
    node.setAttribute("class", "event");
    document.getElementById("2").appendChild(node);
};

$(document).ready(function(){
    $("td").click(function(){
        alert("Ahahahah");
        var node = document.createElement("span");
        node.setAttribute("class", "event");
        document.getElementById(this).appendChild(node);
    });
});

The {% ... %} is from Flask/Jinja stuff, but I'm quite sure that's not the problem. {%...%}来自Flask / Jinja的东西,但是我很确定那不是问题。

The script is in an other file, but the file is included. 该脚本在另一个文件中,但包含该文件。 (Sure about it because the script "addEvent" works well). (可以肯定,因为脚本“ addEvent”运行良好)。

So I tried many, many things, but still not working. 因此,我尝试了很多很多事情,但仍然无法正常工作。 The alert pop-up is not even showing up, so I can't try the script. 警报弹出窗口甚至没有显示出来,因此我无法尝试该脚本。 I guess that the way to inject the new element is wrong, but I didn't saw anything with the way I was looking for. 我猜想注入新元素的方法是错误的,但是我一直没有找到想要的方法。

So here it is, thanks in advance for those who take the time to help me on this 在这里,在此先感谢您抽出宝贵的时间来帮助我

I see 我懂了

onclick="addingEvent()" 

but

function addEvent(){

Mabay this is the problem? 马贝这是问题吗?

You need to use the reference to this and using this can append with it. 您需要使用this的引用,并使用this可以附加它。

 $(document).ready(function(){ $("td").click(function(){ alert("Ahahahah"); var node = document.createElement("span"); node.setAttribute("class", "event"); this.appendChild(node); }); }); 

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

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