Javascript 如何同时设置href和title属性并为其提供回调函数?
我正在学习jquery,但我很难弄清楚如何设置两个属性并同时给它一个回调函数。。。。我有关于如何设置多个属性的代码,我有关于如何给它一个回调函数的代码,但是我如何把它们放在一起。。。 以下是设置多个Atributes的代码:Javascript 如何同时设置href和title属性并为其提供回调函数?,javascript,jquery,Javascript,Jquery,我正在学习jquery,但我很难弄清楚如何设置两个属性并同时给它一个回调函数。。。。我有关于如何设置多个属性的代码,我有关于如何给它一个回调函数的代码,但是我如何把它们放在一起。。。 以下是设置多个Atributes的代码: <script> $(document).ready(function(){ $("button").click(function(){ $("#w3s").attr({ "href" : "http://www.w3schools.c
<script>
$(document).ready(function(){
$("button").click(function(){
$("#w3s").attr({
"href" : "http://www.w3schools.com/jquery",
"title" : "W3Schools jQuery Tutorial"
});
});
});
</script>
</head>
<body>
<p><a href="http://www.w3schools.com" id="w3s">W3Schools.com</a></p>
<button>Change href and title</button>
<p>Mouse over the link to see that the href attribute has changed and a title attribute is set.</p>
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“#w3s”).attr({
“href”:”http://www.w3schools.com/jquery",
“标题”:“W3JQuery教程”
});
});
});
更改href和标题
将鼠标悬停在链接上,查看href属性是否已更改,是否设置了title属性
下面是带有回调函数的代码
<script>
$(document).ready(function(){
$("button").click(function(){
$("#w3s").attr("href", function(i,origValue){
return origValue + "/jquery";
});
});
});
</script>
</head>
<body>
<p><a href="http://www.w3schools.com" id="w3s">W3Schools.com</a></p>
<button>Change href Value</button>
<p>Mouse over the link (or click on it) to see that the value of the href attribute has changed.</p>
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“#w3s”).attr(“href”,函数(i,origValue){
返回origValue+“/jquery”;
});
});
});
更改href值
将鼠标悬停在链接上(或单击链接),查看href属性的值是否已更改
那么我该如何将它们组合在一起呢?提前感谢所有花时间的人。您可以使用链接(关于jQuery链接的更多信息,请参见此处),如下所示:
<script>
$(document).ready(function(){
$("button").click(function(){
$("#w3s").attr("href", function(i,origValue){
return origValue + "/jquery";
}).attr('title','Tutorial');
});
});
</script>
<body>
<p><a href="http://www.w3schools.com" id="w3s">W3Schools.com</a></p>
<button>Change href Value</button>
<p>Mouse over the link (or click on it) to see that the value of the href attribute has changed.</p>
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“#w3s”).attr(“href”,函数(i,origValue){
返回origValue+“/jquery”;
}).attr(“标题”、“教程”);
});
});
更改href值
将鼠标悬停在链接上(或单击链接),查看href属性的值是否已更改
我对您的理解是:
注意:我在本例中禁用了链接。我不确定这是什么上下文,但从页面重定向可能不是预期的结果。当然,可以这样做(将函数存储在变量中,然后将其分配给属性)
工作示例-->
好的,让我解释一下,我想使用回调函数,从第二个代码到第一个代码,我想保留我在回答时如何设置标题AtributeLook,我想你是在搜索像我的aswer这样的东西。谢谢你,我实际上已经上了一堂关于链接的课。我想我必须回去,直到我掌握了链接的概念:)谢谢!谢谢@RobertMesserle你是对的,但是你给我的答案有一些我还不懂的概念,你看,我只是在学习jquery,但是我有点理解你的代码:)啊,对不起,习惯的力量。我将添加更多注释,解释每一步的进展情况。=)非常感谢,非常感谢。我将在进一步了解jquery并再次阅读您的代码(作为我的练习之一)后回来:)谢谢,这是一个更干净的代码,现在我知道了如何让代码更具可读性,多亏了您。。干杯,伙计。。。当我有足够的声望点投票给这个答案时,我会回来:)
//-- $(function () {...}) is shorthand notation for $(document).ready(function () {...});
$(function () {
//-- I stored the lookup for #w3s to prevent duplicate calls to $()
var $link = $('#w3s');
//-- store the original attribute values using jQuery data()
$link.data({
'href': $link.attr('href'),
'title': $link.attr('title')
});
//-- This is essentially the same as your $('button').click(), but using delegation
$(document).on('click', 'button', function () {
//-- update the attributes
$link.attr({
'href': 'http://www.w3schools.com/jquery',
'title': 'W3Schools jQuery Tutorial'
})
//-- on click, reset the original values
$link.on('click', function (event) {
//-- event.preventDefault() will stop the link from performing its default functionality
//-- in this case, that would be following the href
event.preventDefault();
//-- pull the original values from jQuery data()
$link.attr({
'href': $link.data('href'),
'title': $link.data('title')
});
});
});
});
$(document).ready(function(){
$("button").click(function(){
var func= function(i,origValue){
return origValue + "/jquery";
};
$("#w3s").attr({
"href" : func,
"title" : "W3Schools jQuery Tutorial"
});
});
});