简体   繁体   English

在Dragstart事件上无法识别功能

[英]Function not recognised on dragstart event

A function getId is not getting called on dragstart in html. 在HTML的dragstart上未调用函数getId。 The code looks simple and correct, I am not sure why it isn't getting called 该代码看起来简单正确,我不确定为什么没有调用它

HTML code: HTML代码:

<div id="content">
    <div style="position: relative; height: 100%;">
        <div style="display: inline-block; height: 100%; vertical-align: top; border-right: 2px solid darkcyan;" id="sidebar">
            <div><a id="newTask" title="Add new task" draggable="true" ondragstart=getId(this.id)><img src="task.png" /></a></div>
        </div>
<div id="canvas" tabindex="0"></div>
<div id="tabbar"></div>
    </div>
</div>

JS: JS:

window.onload = function ()
{
    var svg = d3.select("body")
            .append("svg")
            .attr("width", 800)
            .attr("height", 803);

    function getId(id)
    {
        var shapeId = id;
        console.log(shapeId);
    }
};

Its throwing an error saying: Uncaught ReferenceError: getId is not defined 它抛出一个错误说: Uncaught ReferenceError: getId is not defined

You've declared your getId function within the onload function - ie it's not in a scope where it can be invoked via the event. 您已经在onload函数中声明了getId函数-即它不在可以通过事件调用的范围内。

Move it outside the onload function: 将其onload函数之外:

window.onload = function ()
{
    var svg = d3.select("body")
            .append("svg")
            .attr("width", 800)
            .attr("height", 803);


};

function getId(id)
{
    var shapeId = id;
    console.log(shapeId);
}

Try this: 尝试这个:

window.onload = function ()
{
    var svg = d3.select("body")
            .append("svg")
            .attr("width", 800)
            .attr("height", 803);
};

    function getId(id)
    {
        var shapeId = id;
        console.log(shapeId);
    }

Function getId is not visible due to function scope it's defined. 由于定义了功能范围,因此无法看到功能getId Make it global. 使其全球化。 (Further refactoring will be required). (将需要进一步的重构)。

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

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