Javascript 如何创建';JQuery';函数调用并将数据传递给它们?
我正在使用qtipjquery插件添加工具提示。我可以在整个网站上添加50-60个工具提示 我现在发现的方法是反复剪切/粘贴jquery代码。例如:Javascript 如何创建';JQuery';函数调用并将数据传递给它们?,javascript,jquery,Javascript,Jquery,我正在使用qtipjquery插件添加工具提示。我可以在整个网站上添加50-60个工具提示 我现在发现的方法是反复剪切/粘贴jquery代码。例如: // First, setup a tool tip for 'Enter Username'. $("input#user_login").qtip({ content: 'Enter username', position: { corner: { target: 'rightMiddle',
// First, setup a tool tip for 'Enter Username'.
$("input#user_login").qtip({
content: 'Enter username',
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
});
现在,重复“名字”的所有内容。似乎毫无意义
$("input#user_login").qtip({
content: 'Enter First Name',
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
});
如何创建一个只需将显示文本传入的函数?如果所有其他选项(如样式和宽度)对于所有工具提示都相同,我建议更改插件代码中的默认值。这将允许您制作如下工具提示:
function setupToolTip(jQueryElement, display) {
jQueryElement.qtip({
content: display,
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
});
}
setupToolTip($("input#user_login"), 'Enter username');
setupToolTip($("input#user_first_name"), 'Enter First Name');
$("input#user_login").qtip({content: "Enter username"});
$("input#user_password").qtip({content: "Enter password"});
var qtip_options = {
content: 'I'm a tooltip,
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
};
$("#user_login").qtip($.extend({}, qtip_options, { content: 'Enter username'});
$("#first_name").qtip($.extend({}, qtip_options, { content: 'Enter First Name'});
$.fn.myQtip = function(text) {
return this.qtip($.extend({}, qtip_options, { content: text});
};
$("#user_login").myQtip('Enter username');
$("#first_name").myQtip('Enter First Name');
如果所有其他选项(如样式和宽度)对于所有工具提示都相同,我建议更改插件代码中的默认值。这将允许您制作如下工具提示:
$("input#user_login").qtip({content: "Enter username"});
$("input#user_password").qtip({content: "Enter password"});
var qtip_options = {
content: 'I'm a tooltip,
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
};
$("#user_login").qtip($.extend({}, qtip_options, { content: 'Enter username'});
$("#first_name").qtip($.extend({}, qtip_options, { content: 'Enter First Name'});
$.fn.myQtip = function(text) {
return this.qtip($.extend({}, qtip_options, { content: text});
};
$("#user_login").myQtip('Enter username');
$("#first_name").myQtip('Enter First Name');
作为一个通用解决方案,任何时候您想要使用默认选项,然后扩展它,您可以在它上使用,如下所示:
$("input#user_login").qtip({content: "Enter username"});
$("input#user_password").qtip({content: "Enter password"});
var qtip_options = {
content: 'I'm a tooltip,
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
};
$("#user_login").qtip($.extend({}, qtip_options, { content: 'Enter username'});
$("#first_name").qtip($.extend({}, qtip_options, { content: 'Enter First Name'});
$.fn.myQtip = function(text) {
return this.qtip($.extend({}, qtip_options, { content: text});
};
$("#user_login").myQtip('Enter username');
$("#first_name").myQtip('Enter First Name');
或者在你的情况下,因为你做了很多,让我们使用一个插件,如下所示:
$("input#user_login").qtip({content: "Enter username"});
$("input#user_password").qtip({content: "Enter password"});
var qtip_options = {
content: 'I'm a tooltip,
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
};
$("#user_login").qtip($.extend({}, qtip_options, { content: 'Enter username'});
$("#first_name").qtip($.extend({}, qtip_options, { content: 'Enter First Name'});
$.fn.myQtip = function(text) {
return this.qtip($.extend({}, qtip_options, { content: text});
};
$("#user_login").myQtip('Enter username');
$("#first_name").myQtip('Enter First Name');
那么就这样称呼它:
$("input#user_login").qtip({content: "Enter username"});
$("input#user_password").qtip({content: "Enter password"});
var qtip_options = {
content: 'I'm a tooltip,
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
};
$("#user_login").qtip($.extend({}, qtip_options, { content: 'Enter username'});
$("#first_name").qtip($.extend({}, qtip_options, { content: 'Enter First Name'});
$.fn.myQtip = function(text) {
return this.qtip($.extend({}, qtip_options, { content: text});
};
$("#user_login").myQtip('Enter username');
$("#first_name").myQtip('Enter First Name');
作为一个通用解决方案,任何时候您想要使用默认选项,然后扩展它,您可以在它上使用,如下所示:
$("input#user_login").qtip({content: "Enter username"});
$("input#user_password").qtip({content: "Enter password"});
var qtip_options = {
content: 'I'm a tooltip,
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
};
$("#user_login").qtip($.extend({}, qtip_options, { content: 'Enter username'});
$("#first_name").qtip($.extend({}, qtip_options, { content: 'Enter First Name'});
$.fn.myQtip = function(text) {
return this.qtip($.extend({}, qtip_options, { content: text});
};
$("#user_login").myQtip('Enter username');
$("#first_name").myQtip('Enter First Name');
或者在你的情况下,因为你做了很多,让我们使用一个插件,如下所示:
$("input#user_login").qtip({content: "Enter username"});
$("input#user_password").qtip({content: "Enter password"});
var qtip_options = {
content: 'I'm a tooltip,
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
};
$("#user_login").qtip($.extend({}, qtip_options, { content: 'Enter username'});
$("#first_name").qtip($.extend({}, qtip_options, { content: 'Enter First Name'});
$.fn.myQtip = function(text) {
return this.qtip($.extend({}, qtip_options, { content: text});
};
$("#user_login").myQtip('Enter username');
$("#first_name").myQtip('Enter First Name');
那么就这样称呼它:
$("input#user_login").qtip({content: "Enter username"});
$("input#user_password").qtip({content: "Enter password"});
var qtip_options = {
content: 'I'm a tooltip,
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
},
style: {
border: {
width: 1,
radius: 3,
color: '#0066CC'
},
width: 200,
name: 'blue'
}
};
$("#user_login").qtip($.extend({}, qtip_options, { content: 'Enter username'});
$("#first_name").qtip($.extend({}, qtip_options, { content: 'Enter First Name'});
$.fn.myQtip = function(text) {
return this.qtip($.extend({}, qtip_options, { content: text});
};
$("#user_login").myQtip('Enter username');
$("#first_name").myQtip('Enter First Name');
您可以按类一次瞄准所有元素,而不是瞄准特定元素。然后在HTML中隐藏这些值。这样,您只需一次调用即可设置qTip,它会自动查找您的内容。当添加更多元素时,您不需要修改脚本,只需给它们一个“hastinp”类,就可以了
$(".hasTip").qtip({
content: $(this).next('.tipText').html(),
...
在HTML中(将其放在类为“Hastinp”的元素后面):
您可以按类一次针对所有元素,而不是针对特定元素。然后在HTML中隐藏这些值。这样,您只需一次调用即可设置qTip,它会自动查找您的内容。当添加更多元素时,您不需要修改脚本,只需给它们一个“hastinp”类,就可以了
$(".hasTip").qtip({
content: $(this).next('.tipText').html(),
...
在HTML中(将其放在类为“Hastinp”的元素后面):
这似乎是标记中的一个巨大的过度使用,为什么不使用数据属性呢?同样,在您的示例中,这个
引用了文档
,您需要一个。each()
查找这个
来引用您要查找的。hastinp
元素。这看起来像是标记中的巨大过度使用,为什么不使用数据属性呢?同样,在您的示例中,此
指的是文档
,您需要一个。each()
查找此
以指代您所追求的。Hastinp
元素。已实现,正在工作,并且正是我所想的。谢谢实施、工作和我所想的完全一样。谢谢