简体   繁体   English

如何在没有eval()的情况下执行存储为字符串的函数

[英]How to execute function stored as a string without eval()

Let me preface this question by saying there may be a completely different solution than my title so let me tell you what I am after entirely. 让我先说这个问题,说可能有一个完全不同于我的标题的解决方案,所以让我告诉你我完全是在追求什么。

Basically I have a series of menu links. 基本上我有一系列的菜单链接。 The href in all the links are storing jQuery functions, not URL's (to control turn.js book script). 所有链接中的href都存储jQuery函数,而不是URL(用于控制turn.js书籍脚本)。 So one of the href 's may look like so: 所以其中一个href可能看起来像这样:

<li id="menu-item-68" class="home menu-item menu-item-type-custom menu-item-object-custom menu-item-68">
  <a href="$('#primary').turn(‘page’,1);"></a>
</li>

The request is simple...on click() of a menu item i need to execute the contents of the href . 请求很简单...在菜单项的click()上我需要执行href的内容。 My first thought was to run through each link and store a temp string of the href and then use eval() but that looks to be a bad option from what I read. 我的第一个想法是遍历每个链接并存储href的临时字符串,然后使用eval()但这看起来是我阅读的一个不好的选择。

So, how can I execute the contents of my href quickly and safely? 那么,如何快速安全地执行我的href内容呢?

There are a few solutions here. 这里有一些解决方案。 You might want to look into using something like Backbone which provides something called routers that would aid you in navigation. 您可能希望使用像Backbone这样的东西来提供一些可以帮助您进行导航的路由器。

Or to hack up something similar, set each of your href tags to point to a new # anchor: 或者为了破解类似的东西,将每个href标记设置为指向一个新的#锚:

<a href="#2">next page</a>

and then attach a listener to the hashchange event. 然后将一个监听器附加到hashchange事件。 When the event fires, the user has clicked on the link. 当事件触发时,用户已单击该链接。 Then access the hash window.location.hash and use that as the argument to a function which does what you need it to do. 然后访问哈希window.location.hash并将其用作函数的参数,该函数执行您需要它执行的操作。

$(window).on('hashchange', function(event){
    $('#primary').turn('page', window.location.hash.slice(1));
}

The slice is there since window.location.hash includes the # character. 由于window.location.hash包含#字符,因此slice就在那里。


EDIT 编辑

I notice you updated saying you use wordpress and you're editing out the http:// part of your URLs. 我注意到您更新了说您使用wordpress并且您正在编辑URL的http://部分。 If you're using the hashchange event you don't need to since href="#2" is the same as href="http://mysite.post.com/this_post/#2" 如果您正在使用hashchange事件,则不需要,因为href="#2"href="http://mysite.post.com/this_post/#2"

How about use the onclick event? 如何使用onclick事件?

<a onclick="function(){$('#primary').turn('page', 2); return false;}" href="#">Click me!</a>

Alternately, since you're using JQuery, you could .bind() the event: 或者,既然你正在使用JQuery,你可以.bind()事件:

<script>
    $('a.clickable').bind('click', function(e){
         var pageNum = $(this).attr('data-page');
         $(#primary).turn('page', pageNum);
         return false;
    });
</script>
<body>
   <a class='clickable' data-page='2' href='#'>Click me!</a>

Alternately, since you only have access to the href attribute, you COULD do this, which is the easy way out: 或者,由于您只能访问href属性,因此您可以执行此操作,这是一种简单的方法:

 <a href="javascript:$('#primary').turn('page', 2)">Click me!</a>

Can't you simply simulate a click event on the links in that case? 在这种情况下,你不能简单地模拟链接上的点击事件吗? In other words, if you already have jquery loaded $("a#identifier").click() . 换句话说,如果你已经加载$("a#identifier").click() jquery $("a#identifier").click() In your current scenario that seems to be the easiest way. 在您当前的场景中,这似乎是最简单的方法。

I've a similar issue and solved it like this: 我有类似的问题并解决了这个问题:

In your script, define a function like: 在您的脚本中,定义一个函数,如:

$.fn.DoSomething=function (MyOption){ window.alert(MyOption); };

And in your link href call the function like javascript:$.fn.DoSomething('Hello'); 在你的链接href中调用函数,如javascript:$.fn.DoSomething('Hello');

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

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