Uncaught ReferenceError:

huangapple go评论64阅读模式
英文:

Uncaught ReferenceError:

问题

我有这段JavaScript代码。但是当我点击“保存”按钮时,控制台会报错:“未捕获的引用错误:saveMarker未定义”。我是从我的“主”HTML文件中的脚本块调用下面的代码的。弹出窗口正常工作,但当我点击保存时会出现错误。

我在Leaflet库中使用它,如果有人想知道的话,它是通过“L”引用的。我还添加了项目的图片。

我尝试了这个版本:

```javascript
export function markerpopup(map, latlng) {
    console.log("开始 markerpopup");
    var actual_location = "";
    var popupContent = `
      <button onclick="saveMarker()">保存</button>`;
    L.popup()
        .setLatLng(latlng)
        .setContent(popupContent)
        .openOn(map);
}

function saveMarker() {
    console.log("开始 saveMarker");
}

和这个版本(应该与https://www.w3schools.com/JSREF/event_onclick.asp相同):

export function markerpopup(map, latlng) {
    console.log("开始 markerpopup");
    var actual_location = "";

   var popupContent = `
            <button onclick="saveMarker()">保存</button>

            <script> 
                function saveMarker() {
                    console.log("开始 saveMarker");
                }
            </script>`;

    L.popup()
        .setLatLng(latlng)
        .setContent(popupContent)
        .openOn(map);
}

如果有人能帮忙,将不胜感激!


<details>
<summary>英文:</summary>

Hi i have this JavaScript Code. But i get the error: **`&quot;Uncaught ReferenceError: saveMarker is not defined&quot;`** from the console when i click the &quot;save&quot; button. I call the code below from my &quot;main&quot; HTML file with a script block. The Popup is functioning but when i klick on save the error comes up.

I use it with the leaflet library which is referenced by the &quot;L&quot; if someone is wondering. I also added a picture of the project.

I Tried this Version:

export function markerpopup(map, latlng) {
console.log("starte markerpopup");
var actual_location = "";
var popupContent =
&lt;button onclick=&quot;saveMarker()&quot;&gt;Speichern&lt;/button&gt;
;
L.popup()
.setLatLng(latlng)
.setContent(popupContent)
.openOn(map);
}

function saveMarker() {
console.log("starte saveMarker");
}


And this Version (Which should be the same as https://www.w3schools.com/JSREF/event_onclick.asp):

export function markerpopup(map, latlng) {
console.log("starte markerpopup");
var actual_location = "";

var popupContent = `
<button onclick="saveMarker()">Speichern</button>

        &lt;script&gt; 
            function saveMarker() {
                console.log(&quot;starte saveMarker&quot;);
            }
        &lt;/script&gt;`;

L.popup()
    .setLatLng(latlng)
    .setContent(popupContent)
    .openOn(map);

}


Would appreciate if someone could help!

Tried different places to place the `saveMarker` function.

</details>


# 答案1
**得分**: 1

将字符串传递给[`setContent()`][1]的方式,你可以传递一个[`HTMLElement`][2]。

你可以通过调用[`document.createElement()`][3]来创建一个HTMLElement,然后使用[`addEventListener()`][4]附加事件处理程序,并使用[`append()`][5]、[`innerHTML`][6]或任何其他方法来更新`HTMLElement`的内容。

``` js
export function markerpopup(map, latlng) {
    console.log("开始 markerpopup");

    var actual_location = "";

    const popupContent = document.createElement("button");
    popupContent.addEventListener("click", saveMarker);
    popupContent.append("保存");
    // 或者,如果你想要一个更复杂的结构,而不是手动构建所有元素
    // popupContent.innerHTML = `<span>保存</span>`;

    L.popup()
        .setLatLng(latlng)
        .setContent(popupContent)
        .openOn(map);
}

function saveMarker() {
    console.log("开始 saveMarker");
}
英文:

Instead of passing a string to setContent() you could pass an HTMLElement.

You can create one by calling document.createElement(), then attach the event handler with addEventListener() and set the content with append(), innerHTML, or any other method to update the contents of an HTMLElement.

export function markerpopup(map, latlng) {
    console.log(&quot;starte markerpopup&quot;);

    var actual_location = &quot;&quot;;

    const popupContent = document.createElement(&quot;button&quot;);
    popupContent.addEventListener(&quot;click&quot;, saveMarker);
    popupContent.append(&quot;Speichern&quot;);
    // or if you want a more complex structure without manually building all elements
    // popupContent.innerHTML = `&lt;span&gt;Speichern&lt;/span&gt;`;

    L.popup()
        .setLatLng(latlng)
        .setContent(popupContent)
        .openOn(map);
}

function saveMarker() {
    console.log(&quot;starte saveMarker&quot;);
}

答案2

得分: 0

由于您正在使用export语法,我假设您正在使用ES6模块,并且有一个<script type="module">标签。如果这是正确的,那么您的函数声明是在模块中作用域,而不是整个页面。您需要像这样做:


// 为自己设置按钮的ID
// 这只是一个占位符
document.querySelector('#popupbutton').onclick = saveMarker

function saveMarker() {
    console.log("开始保存标记");
}

英文:

Since you are using export syntax, I assume that you are using an es6 module
, and a &lt;script type=&quot;module&quot;&gt; tag. if this is true, then your function declaration is scoped in the module, and not the whole page. You need to do something like this:


    //set the button id for yourself
    //this is just a placeholder
    document.querySelector(&#39;#popupbutton&#39;).onclick = saveMarker
    
    function saveMarker() {
        console.log(&quot;started saveMarker&quot;);
    }

答案3

得分: 0

以下是翻译好的代码部分:

function markerpopup(map, latlng) {
    console.log("开始 markerpopup");

    var actual_location = "";

    var popupContent = `
        <button id="savemarkerId">保存</button>
    `;

    const saveMarker = () => {
        console.log("开始保存标记");
    }

    L.popup()
        .setLatLng(latlng)
        .setContent(popupContent)
        .openOn(map);

    document.querySelector('#savemarkerId').onclick = saveMarker
}
function markerpopup(map, latlng) {
    console.log("开始 markerpopup");

    var actual_location = "";

    var button = document.createElement('button');

    button.innerHTML = '点击我';
    button.onclick = function() {
        alert("你好,世界");
    }

    L.popup()
        .setLatLng(latlng)
        .setContent(button)
        .openOn(map);
}
英文:

what you can try to do is

function markerpopup(map, latlng) {
    console.log(&quot;starte markerpopup&quot;);

    var actual_location = &quot;&quot;;

    var popupContent = `

            &lt;button id=&quot;savemarkerId&quot; &gt;Speichern&lt;/button&gt;
              
    `;

    const saveMarker = () =&gt; {
        console.log(&quot;started saveMarker&quot;);
    }


    L.popup()
        .setLatLng(latlng)
        .setContent(popupContent)
        .openOn(map);

    document.querySelector(&#39;#savemarkerId&#39;).onclick = saveMarker

}

or you can try

    function markerpopup(map, latlng) {
        console.log(&quot;starte markerpopup&quot;);
    
        var actual_location = &quot;&quot;;
    
var button = document.createElement(&#39;button&#39;);

button.innerHTML = &#39;click me&#39;;
button.onclick = function()
{
    alert(&quot;hello, world&quot;);
}

        L.popup()
            .setLatLng(latlng)
            .setContent(button)
            .openOn(map);
    
    
    }

huangapple
  • 本文由 发表于 2023年7月14日 00:06:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定