[英]Javascript - how do I minimise this code example?
a quick question. 一个快速的问题。
At the moment I have 12 links on a page, and 12 corresponding javascript codes that run when a each button is clicked. 目前,我在页面上有12个链接,以及单击每个按钮时运行的12个相应的javascript代码。
I know 100% there must be a method of having 1 javascript code and the link passing a variable to it, so I don't have to have 12 different codes. 我知道100%必须有一种具有1个javascript代码和将变量传递给它的链接的方法,因此我不必具有12个不同的代码。
EG. 例如。 Here is a link I'm currently using: 这是我当前正在使用的链接:
<a href="#" id="button1">Anatomical Pathology</a>
And the Javascript function that is run when the link is clicked loads some html from a php script into a div which is previously defined as level2: 单击链接时运行的Javascript函数将php脚本中的html加载到div中,该div先前定义为level2:
$('#button1').click(function() {
level2.load("http://hello/script.php?url=poodles");
});
What I'd really like to do is something like this with the link: 我真正想做的是带有以下链接的内容:
<a href="#" id="button1" passurl="poodles">Anatomical Pathology</a>
And the function something like this, so I only need the 1 function not 12: 函数是这样的,所以我只需要1函数而不是12:
$('#button1').click(function() {
level2.load("http://hello/script.php?url=' + passurl + '");
});
How do I go about getting the data from the link tag into javascript, and also how do I add this passed variable into the url I want the javascript to pull data in from? 如何将链接标记中的数据导入javascript,以及如何将此传递的变量添加到希望javascript提取数据的url中?
passurl
isn't standard attribute, you should use data-passurl
passurl
不是标准属性,您应该使用data-passurl
$('#button1').click(function() {
var passurl = $(this).data('passurl'); // or $(this).attr('data-passurl');
level2.load("http://hello/script.php?url=" + passurl);
});
Why don't you utilize your hash there... 你为什么不在那里利用你的哈希...
<a href="#/poodles" class="button">Anatomical Pathology</a>
In your script 在您的脚本中
$(".button").each(function() {
// get the hash and extract the part we want store it in this enclosure
var url = $(this).attr("href").replace(/^#\//, "");
// create a click handler that loads the url
$(this).click(function() {
level2.load("http://hello/script.php?url=" + url);
});
});
This also brings about the possibility to extrapolate from that so that a hash passed through the url can also operate the script loading... 这也带来了从中推断的可能性,以便通过url传递的哈希也可以操作脚本加载...
You can use the rel attributte (or any data- attributes if your using HTML5 Doctype) to save your URL and add a class to the links you want to execute your callback. 您可以使用rel属性(如果使用HTML5 Doctype,则可以使用任何data-attribute)来保存URL并将类添加到要执行回调的链接中。
<a href="#" class="button" rel="your-url">Anatomical Pathology</a>
Your Callback: 您的回电:
$('a.button').click(function() {
level2.load("http://hello/script.php?url=' + $(this).attr('rel') + '");
});
For a more extensible solution you could consider making a json structure for your urls: 对于更可扩展的解决方案,您可以考虑为您的网址建立一个json结构:
var urls = [{
"poodle":{
"url":"http://hello/script.php?url=poodle",
"someOtherData":"data"
},
"otherDog":{
"url":"http://hello/script.php?url=otherDog",
"someOtherData":"data"
}
}];
You would store some sort of key somewhere in your HTML element: 您将在HTML元素中的某处存储某种密钥:
<a href="#" rel="poodle">Anatomical Pathology</a>
Then you would leverage this data structure in your functional code: 然后,您可以在功能代码中利用此数据结构:
$('a').click(function () {
var key = $(this).attr('rel');
level2.load(urls[key].url);
});
As per Stuie, add a class to the links so that you can target them all at once. 根据Stuie,将类添加到链接中,以便您可以一次将它们全部定位。 However, I don't see the need to fool with the hash, and I wouldn't add a bunch of click events either. 但是,我认为没有必要弄混哈希值,也不会添加任何click事件。 Just delegate once and you're done: 只需委托一次,您就可以完成:
<div id="wrapper">
<a href="poodles" class="button">Poodles</a>
<a href="schnausers" class= "button">Schnausers</a>
</div>
and the JS: 和JS:
$('#wrapper').delegate('a.button', 'click', function(e) {
e.preventDefault();
var passurl = $(this).attr("href");
level2.load("http://hello/script.php?url=" + passurl); // assuming "level2" is valid from elsewhere
});
Where I have "#wrapper" you would designate any unique selector that is an ancestor of all your links. 在有“ #wrapper”的地方,您将指定任何唯一选择器,该选择器是所有链接的祖先。 It's an element that listens for clicks on the a.button elements within. 它是一个侦听其中的a.button元素的单击的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.