带参数的Onclick会导致javascript语法错误

时间:2015-12-15 00:13:43

标签: javascript jquery html openlayers-3

单击id为“elem”的元素时,我收到了“Uncaught SyntaxError:Unexpected identifier”。你能告诉我如何解决这个问题吗?

formulaireDB.allDocs({
    include_docs: true
}).then(function(result) {

    for (x in result.rows) {
        docFeature = result.rows[x].doc;
        coord_nv = [docFeature.geometry.coordinates[0], 
                    docFeature.geometry.coordinates[1]];
         position = to3857(coord_nv);

        map.addOverlay(new ol.Overlay({
            position: ol.proj.transform(
                [docFeature.geometry.coordinates[0],
                docFeature.geometry.coordinates[1]],
                'EPSG:4326',
                'EPSG:3857'),
                 element: $('<img id="elem" onclick="clickmarker(' 
                           + result + ',' + x  + ')" src="./img/pin32.png">')
        }));
    }
}).catch(function(err) {
    console.log(err);
});

}

function clickmarker(rt, ind) {
    var res = rt.rows[ind].doc.geometry.coordinates;
    console.log(res);
}

2 个答案:

答案 0 :(得分:0)

你的捕捉功能后你有一个额外的支撑。试试这个:

&#13;
&#13;
formulaireDB.allDocs({
  include_docs: true
}).then(function(result) {


  for (x in result.rows) {
    docFeature = result.rows[x].doc;
    coord_nv = [docFeature.geometry.coordinates[0], docFeature.geometry.coordinates[1]];
    position = to3857(coord_nv);
    // add marker

    map.addOverlay(new ol.Overlay({
      position: ol.proj.transform(
        [docFeature.geometry.coordinates[0], docFeature.geometry.coordinates[1]],
        'EPSG:4326',
        'EPSG:3857'
      ),
      element: $('<img id="elem" onclick="clickmarker(' + result + ',' + x + ')" src="./img/pin32.png">')
    }));



  }
}).catch(function(err) {
  console.log(err);
});



function clickmarker(rt, ind) {
  var res = rt.rows[ind].doc.geometry.coordinates;
  console.log(res);
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您正在将result连接到字符串,并将其值作为此行中的对象丢失:

element: $('<img id="elem" onclick="clickmarker(' + result + ',' + x  + ')" src="./img/pin32.png">')

您可能需要在创建点击侦听器之后添加点击侦听器,而不是内联。

诸如

之类的东西
element: $('<img...>').on('click',function(){ clickmarker(result,x); });