Javascript 检查数组的值是否大于。而不是其他样式
Iam目前正在使用jquery mobile,并且有一个动态通知面板。因此,我想检查文本中的字符数,以建立样式。例如,如果通知的Javascript 检查数组的值是否大于。而不是其他样式,javascript,jquery,css,Javascript,Jquery,Css,Iam目前正在使用jquery mobile,并且有一个动态通知面板。因此,我想检查文本中的字符数,以建立样式。例如,如果通知的文本大于10,则将高度设置为Xpx 但我首先要做的是: for(var count = 0; count < info.data.length; count++){ var shortmessage = info.data[count][3]; var category = info.data[count][4]; if(category
文本大于10
,则将高度设置为Xpx
但我首先要做的是:
for(var count = 0; count < info.data.length; count++){
var shortmessage = info.data[count][3];
var category = info.data[count][4];
if(category === 'douane'){
douaneHtml = douaneHtml + "<div class='notification-item'>" +
"<div class='ui-grid-a notification-grid'>" +
"<div class='ui-block-a'>" +
"<img class='notification-image' src=" + imgPath + ">"+
"</div>" +
"<div class='ui-block-b'>" +
"<span class='notification-text'>" + shortmessage + "</span>" +
"</div>" +
"</div>";
$('.douane-notification-append').empty().prepend(douaneHtml);
}
}
但是当我执行console.log(shortmessage.val())时代码>在if(category=='douane')中
我会得到这个作为回报:
shortmessage.val is not a function
有人能帮我解决这个问题吗?基本上,我想做的是计算短消息中的字符数,然后根据这些字符做不同的样式
这是console.log(info.data[count])的输出代码>
问题:val()
方法无法对字符串调用,因为短消息
包含将引发错误的字符串消息
建议的解决方案:只需删除额外的.val()
即可:
if ( shortmessage.length > 10 ){
$('.notification-item').css('min-height', '100px');
}
注意:length
是一个属性,因此您应该从长度中删除()
希望这有帮助。问题:val()
方法无法对字符串调用,因为短消息
包含将引发错误的字符串消息
建议的解决方案:只需删除额外的.val()
即可:
if ( shortmessage.length > 10 ){
$('.notification-item').css('min-height', '100px');
}
注意:length
是一个属性,因此您应该从长度中删除()
希望这有帮助。短消息
是一个字符串
,您需要阅读长度
属性:
- 我已经清理了字符串生成,使用字符串数组,然后
加入它们。好多了李>
- 检查它是否太短,并更改
样式
变量
- 然后在模板中使用这个
for(var count = 0; count < info.data.length; count++){
var shortmessage = info.data[count][3];
var category = info.data[count][4];
var style = '';
// if long message, set the style
if ( shortmessage.length > 10 ){
style = 'min-height: 100px';
}else if ( shortmessage.length > 20 ){
style = 'min-height: 200px';
}else if ( shortmessage.length > 30 ){
style = 'min-height: 300px';
}
if(category === 'douane'){
douaneHtml = [
douaneHtml,
"<div class='notification-item' style='" + style + "'>",
"<div class='ui-grid-a notification-grid'>",
"<div class='ui-block-a'>",
"<img class='notification-image' src=" + imgPath + ">",
"</div>",
"<div class='ui-block-b'>",
"<span class='notification-text'>" + shortmessage + "</span>",
"</div>",
"</div>"
].join('');
$('.douane-notification-append').empty().prepend(douaneHtml);
}
}
for(var count=0;count10){
样式='最小高度:100px';
}否则如果(shortmessage.length>20){
样式='最小高度:200px';
}否则如果(shortmessage.length>30){
样式='最小高度:300px';
}
如果(类别=='douane'){
douaneHtml=[
douaneHtml,
"",
"",
"",
"",
"",
"",
“+短消息+”,
"",
""
].加入(“”);
$('.douane notification append').empty().prepend(douaneHtml);
}
}
如果可以,请尝试新的ES2015字符串。
大量清理代码
douaneHtml = `
${douaneHtml}
<div class='notification-item' style='${style}'>
<div class='ui-grid-a notification-grid'>
<div class='ui-block-a'>
<img class='notification-image' src='${imgPath}'>
</div>
<div class='ui-block-b'>
<span class='notification-text'>${shortmessage}</span>
</div>
</div>`;
douaneHtml=`
${douaneHtml}
${shortmessage}
`;
短消息
是一个字符串
,您需要阅读长度
属性:
- 我已经清理了字符串生成,使用字符串数组,然后
加入它们。好多了李>
- 检查它是否太短,并更改
样式
变量
- 然后在模板中使用这个
for(var count = 0; count < info.data.length; count++){
var shortmessage = info.data[count][3];
var category = info.data[count][4];
var style = '';
// if long message, set the style
if ( shortmessage.length > 10 ){
style = 'min-height: 100px';
}else if ( shortmessage.length > 20 ){
style = 'min-height: 200px';
}else if ( shortmessage.length > 30 ){
style = 'min-height: 300px';
}
if(category === 'douane'){
douaneHtml = [
douaneHtml,
"<div class='notification-item' style='" + style + "'>",
"<div class='ui-grid-a notification-grid'>",
"<div class='ui-block-a'>",
"<img class='notification-image' src=" + imgPath + ">",
"</div>",
"<div class='ui-block-b'>",
"<span class='notification-text'>" + shortmessage + "</span>",
"</div>",
"</div>"
].join('');
$('.douane-notification-append').empty().prepend(douaneHtml);
}
}
for(var count=0;count10){
样式='最小高度:100px';
}否则如果(shortmessage.length>20){
样式='最小高度:200px';
}否则如果(shortmessage.length>30){
样式='最小高度:300px';
}
如果(类别=='douane'){
douaneHtml=[
douaneHtml,
"",
"",
"",
"",
"",
"",
“+短消息+”,
"",
""
].加入(“”);
$('.douane notification append').empty().prepend(douaneHtml);
}
}
如果可以,请尝试新的ES2015字符串。
大量清理代码
douaneHtml = `
${douaneHtml}
<div class='notification-item' style='${style}'>
<div class='ui-grid-a notification-grid'>
<div class='ui-block-a'>
<img class='notification-image' src='${imgPath}'>
</div>
<div class='ui-block-b'>
<span class='notification-text'>${shortmessage}</span>
</div>
</div>`;
douaneHtml=`
${douaneHtml}
${shortmessage}
`;
我们必须查看数据
内容。从外观上看,短消息
不是jquery对象,因此它就像您的消息表明它没有val
方法一样。我对我的帖子进行了编辑,以便您看到输出内容。所有字符串我们都必须查看数据
内容。从外观上看,短消息
不是jquery对象,因此它就像您的消息表明它没有val
方法一样。我对我的帖子进行了编辑,以便您看到输出内容。所有字符串未捕获类型错误:shortmessage.length不是一个函数
它告诉我错误length
是一个属性,而不是一个方法.length
不是.length()
。。。长度是字符串的属性,而不是函数……看看其他答案。@PhilPoore想成为StackOverflow的玩家,下次你可以用拼写错误编辑帖子,而不是不使用拼写错误发布相同的答案。.我的错,没有意识到我可以编辑其他帖子。下次我会知道的。谢谢兄弟DUncaught TypeError:shortmessage.length不是一个函数
它告诉我错误length
是一个属性,而不是一个方法.length
不是.length()。。。长度是字符串的属性,而不是函数……看看其他答案