【问题标题】:Conditional without event有条件的无事件
【发布时间】:2015-02-21 01:04:33
【问题描述】:

如果我使用 click、keyup... 功能,此代码将有效。但是如何让它在没有键盘或点击的情况下工作。我的意思是,如何在页面加载时更改行高?

(注意div有不同的高度,我已经有一个jQuery文档准备功能)

小提琴:http://jsfiddle.net/n32u4gnq/1/

$(".note").keyup(function(){
	var noteHeight = $(this).height();
		
	if (noteHeight == 25) {
		$(this).css("line-height","25px");
	} else {	
		$(this).css("line-height","normal");
	}
});
#wrap { 
	margin:0 auto;
	max-width:420px;
}

.note {
	width:100%;
	min-height:25px; line-height:25px;
	margin-bottom:5px;
	padding:0px 10px;
	text-align:left;
	outline:none;
	display:inline-block;
	background:whiteSmoke;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrap" >
	<div class="note" id="primer" contenteditable="true"> First line, First line, First line, First line, First line, First line, First line, First line, First line, First line, First line, First line, First line, First line, First line, First line,  </div>
	<div class="note" contenteditable="true"> Second line </div>
</div>

【问题讨论】:

  • 不清楚你在期待什么,恕我直言
  • 我想你可以停在“我希望它使文本居中”,那么你只需将.note选择器的text-align: left;更改为text-align: center;否则,如果还有更多对于这个问题,你真的需要澄清你的意图。
  • 好的。我已经简化了这个问题。查看更新
  • RE:“就在页面加载时”-$(document).ready(function() { //your code here });

标签: jquery css


【解决方案1】:
$(document).ready(function(){
   $(".note").trigger('keyup');
});

这是为您的代码执行此操作的方法,但是,我会将您的代码分开,以便 line-height 函数是一个名为 changeLineHeight 的单独函数......然后您可以通过 keydown、keyup 等触发它只需使用 changeLineHeight() 在 doc.ready 中运行即可。

例如

var changeLineHeight = function(){
    $(".notes").each(function(i,el){
        var noteHeight = $(this).height();
        if (noteHeight == 25) {
            $(this).css("line-height","25px");
        } else {    
            $(this).css("line-height","normal");
        }
    });
};
$(document).on('keyup keydown paste click', changeLineHeight)
.ready(function(){
    changeLineHeight();
});

【讨论】:

  • $(".note").trigger('keyup');似乎是一个简单聪明的解决方案。但这似乎不起作用。我错过了什么吗? jsfiddle.net/n32u4gnq/11
  • 这里我用函数测试了第二个选项。它不起作用:jsfiddle.net/n32u4gnq/13
  • @Nrc 这里也是第一个固定的。您在定义事件之前调用了触发器,但是使用 jquery 您可以将它们链接起来jsfiddle.net/n32u4gnq/16
  • 这是非常好的选择,而且效果很好。函数中的 (i, el} 是什么?
  • @Nrc 每个函数每次迭代返回变量,i 是整数,el 是元素。所以我去 0,1,2,3,4,5 等等,$(elem) 和 $(this) 一样
【解决方案2】:

只需将您的代码包装在$(document).ready() 事件中,然后使用.each() 函数迭代您的元素。

这样,您的代码将在文档准备好并加载时按您的需要运行。

由于您的元素在 CSS 中已经包含 line-height: 25px;,因此您可以根据需要进行更改:

$(document).ready(function() {
    $('.note').each(function() {
        var noteHeight = $(this).height();

        if (noteHeight > 25)
            $(this).css("line-height", "normal");
    });
});

JSFiddle:http://jsfiddle.net/n32u4gnq/14/

试一试,如果有帮助,请告诉我!

【讨论】:

  • 当然。但它不起作用:jsfiddle.net/n32u4gnq/3 我认为问题在于并非所有音符高度都相同
  • 抱歉,我没有注意到您有多个笔记。看我的更新! :)
【解决方案3】:

将代码包装在$(document).ready() 中就可以了,因为 DOM 应该在操作之前完全加载

$(document).ready(function() {
 $(".note").each(function(){
   var noteHeight = $(this).height();

    if (noteHeight == 25) {
        $(this).css("line-height","25px");
    } else {    
        $(this).css("line-height","normal");
    }
  });
});

工作Fiddle

【讨论】:

    【解决方案4】:

    问题在于您获取的是文档的高度而不是元素的高度。尝试使用此代码代替您的文档侦听器:

    $(document).ready(function() {
    
        var primer=document.getElementById("primer");
        var noteHeight = primer.clientHeight;
        //alert(noteHeight);
        // Your note height might not always be == to 75 or 25.
        // Maybe use <= or >= to work with a wider range of values.
        if (noteHeight == 75) {
                $(primer).css("line-height","100px");
            } else {    
                $(primer).css("line-height","normal");
        }
    
    });
    

    【讨论】:

    • 这不是重点。我使用 jQuery $(function() 或 $(document).ready(function() 并且它不起作用。请检查小提琴:jsfiddle.net/n32u4gnq/3
    • 在你的事件监听器中$(this).height() 返回一个远大于 25 的数字,所以你的行高将始终设置为正常。我认为这是因为在您的侦听器上下文中的“this”指的是文档而不是您的元素。
    • 我更新了我的答案,它在小提琴中有效,所以看看吧!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2013-09-16
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多