在单击超链接之前阻止函数调用

时间:2016-02-17 01:20:08

标签: javascript jquery-mobile hyperlink

我的问题是该功能在我点击超链接之前运行。 在这里我动态创建链接:

   $("#nearByCitiesDiv").append('<a data-role="button"  href="#/" onclick="' + showNearCityWeather(nearbyPosition) + '; return false;" data-theme="a">' + c.toponymName + '</a >'); 

这是函数:

var showNearCityWeather = function (pos)
        {
        alert("asd");
        }

1 个答案:

答案 0 :(得分:1)

它假定您要将函数返回的值附加到onclick。将代码更改为此。

onclick="showNearCityWeather('+nearbyPosition+')"

所以你的完整代码必须是。

$("body").append('<a data-role="button"  href="#/" onclick="showNearCityWeather('+nearbyPosition+'); return false;" data-theme="a">' + c.toponymName + '</a >'); 

注意函数名称只是字符串而不是实际函数调用的更改。

相关问题