我想结合这两个功能。
有人可以告诉我怎么做?我试过了,但我找不到一个好的解决方案。 这些函数执行相同的任务。所以我想把它们结合起来。
详细说明: -
这是我的代码: -
var bg = $("#metadescription").on('keyup', function(){
var maxlength = $(this).attr("data-limit");
var percentage = Math.floor($(this).val().length / maxlength * 100) + '%';
$(".spanbg1").css("width", percentage);
var $span1 = $(".spanbg1");
var lengthScope = 10;
if ($(this).val().length > maxlength)
{
$span1.css("background-color", "#fc3636");
}
else if ($(this).val().length > maxlength-lengthScope)
{
$span1.css("background-color", "#ffb14d");
}
else
{
$span1.css("background-color", "#76e07a");
}
});
var bg = $("#title").on('keyup', function(){
var maxlength = $(this).attr("data-limit");
var lengthScope = 10;
var $span = $(".spanbg");
var percentage = Math.floor($(this).val().length / maxlength * 100) + '%';
$span.css("width", percentage);
if ($(this).val().length > maxlength)
{
$span.css("background-color", "#fc3636");
}
else if ($(this).val().length > maxlength-lengthScope)
{
$span.css("background-color", "#ffb14d");
}
else
{
$span.css("background-color", "#76e07a");
}
});
答案 0 :(得分:2)
var bg = $("#metadescription, #title").on('keyup', function(){
//your common code here
var $span = ($(this).attr('id')=="metadescription" ? $(".spanbg1") : $(".spanbg"));
//your common code here
});
使用@iHasCodeForU 三元表达式
更新了答案答案 1 :(得分:0)
试试这个, 直接放入函数中常见的代码和其他代码放在里面如果条件类似
if($(this).attr("id") == "title") {
// code related to title id
}
else {
}
var bg = $("#metadescription,#title").on('keyup', function(){
var maxlength = $(this).attr("data-limit");
var lengthScope = 10;
var $span = $(".spanbg");
var $span1 = $(".spanbg1");
var percentage = Math.floor($(this).val().length / maxlength * 100) + '%';
if($(this).attr("id") == "title") {
$span.css("width", percentage);
if ($(this).val().length > maxlength) {
$span.css("background-color", "#fc3636");
}
else if ($(this).val().length > maxlength-lengthScope) {
$span.css("background-color", "#ffb14d");
}
else {
$span.css("background-color", "#76e07a");
}
}
else {
if ($(this).val().length > maxlength) {
$span1.css("background-color", "#fc3636");
}
else if ($(this).val().length > maxlength-lengthScope) {
$span1.css("background-color", "#ffb14d");
}
else {
$span1.css("background-color", "#76e07a");
}
}
});
答案 2 :(得分:0)
请试一试。
var bg = $("#metadescription, #title").on('keyup', function() {
var maxlength = $(this).attr("data-limit");
var lengthScope = 10;
var targetSpan = $(this).attr("data-target-span");
var $span = $("."+targetSpan);
var percentage = Math.floor($(this).val().length / maxlength * 100) + '%';
$span.css("width", percentage);
if ($(this).val().length > maxlength) {
$span.css("background-color", "#fc3636");
} else if ($(this).val().length > maxlength - lengthScope) {
$span.css("background-color", "#ffb14d");
} else {
$span.css("background-color", "#76e07a");
}
});
HTML代码:
<input type="text" id="metadescription" data-target-span="spanbg1" >
<span class="spanbg1"></span>
<input type="text" id="title" data-target-span="spanbg" >
<span class="spanbg"></span>