问题描述
我被困住了.搜索并尝试了几个小时.
I am stuck. Searched and tried for hours.
我仍然无法使其正常工作.好的,我只放置源代码以明确说明我要完成的工作.
I still can't make it work. Okay, I'll just put the source code to make it clear what I want to accomplish.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
var date_fmt="yyyy-mm-dd";
var time_fmt="HH:MM";
var date_field="#id_start_0, #id_end_0"; //id refering to html input type='text'
var time_field="#id_start_1, #id_end_1"; //id refereing to html input type='text'
function clearFmt(fmt_type)
{
if($(this).val() == fmt_type) {
$(this).val("");
}
}
function putFmt(fmt_type)
{
if($(this).val() == "") {
$(this).val(fmt_type);
}
}
$(document).ready(function() {
$(date_field).attr("title",date_fmt);
$(time_field).attr("title",time_fmt);
$(date_field).click(function() {
clearFmt(date_fmt);
});
$(date_field).blur(function(){
putFmt(date_fmt);
});
$(time_field).click(function(){
clearFmt(time_fmt);
});
$(time_field).blur(function(){
putFmt(time_fmt);
});
});
</script>
帮助?
推荐答案
使用jquery bind 方法:
Use the jquery bind method:
function myfunc(param) {
alert(param.data.key);
}
$(document).ready( function() {
$("#foo").bind('click', { key: 'value' }, myfunc);
});
另请参阅我的 jsfiddle .
===更新===
自jquery 1.4.3起,您还可以使用:
since jquery 1.4.3 you also can use:
function myfunc(param) {
alert(param.data.key);
}
$(document).ready( function() {
$("#foo").click({ key: 'value' }, myfunc);
});
另请参阅第二篇 jsfiddle .
===更新2 ===
=== UPDATE 2 ===
每个功能都有自己的this
.在此函数中调用clearFmt
后,this
不再是单击的元素.我有两个类似的解决方案:
Each function has his own this
. After calling clearFmt
in this function this
is no longer the clicked element. I have two similar solutions:
在函数中添加一个参数,例如element
并将$(this)
替换为element
.
In your functions add a parameter called e.g. element
and replace $(this)
with element
.
function clearFmt(element, fmt_type) {
if (element.val() == fmt_type) {
element.val("");
}
}
调用该函数必须添加参数$(this)
.
Calling the function you have to add the parameter $(this)
.
$(date_field).click(function() {
clearFmt($(this), date_fmt);
});
另请参阅我的第三个jsfiddle .
-=-
替代方法:
function clearFmt(o) {
if ($(o.currentTarget).val() == o.data.fmt_type) {
$(o.currentTarget).val("");
}
}
$(date_field).click({fmt_type: date_fmt}, clearFmt);
另请参阅我的第四本jsfiddle .
这篇关于如何在jQuery事件处理程序中使用参数调用JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!