简体   繁体   English

如何将我的代码更改为jQuery而不是纯JS?

[英]How can I change my code to jQuery instead of pure JS?

I am trying to set a function on jQuery instead of pure JS and I am getting an error: 我试图在jQuery而非纯JS上设置一个函数,但出现错误:

 15 | prev.css('height') = prev.scrollHeight + "px";
                                               ^ Expected an assignment or function call and instead saw an expression.

This is the actual code: 这是实际的代码:

 function expand(target) { let prev = target.previousElementSibling; prev.style.height = prev.scrollHeight + "px"; target.style.display = "none"; } 
 #p { height: 50px; overflow: hidden; } #read-more { background: linear-gradient(to bottom, rgba(255,0,0,0), rgba(255,255,255,1)); color: blue; cursor: pointer; position: absolute; bottom: -20px; padding: 15px 0; text-align: center; width: 100%; } #wrapper { position: relative; width: 200px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='wrapper'> <p id='p'>Pinterest taxidermy et heirloom, ennui enim eu bicycle rights fugiat nesciunt commodo. High Life food truck jean shorts in. Blog asymmetrical cold-pressed photo booth. Neutra chia in, mustache Etsy nostrud plaid kogi. Magna polaroid stumptown aliqua put a bird on it gentrify, street art craft beer bicycle rights skateboard. DIY plaid gentrify, sustainable sapiente seitan mumblecore viral cardigan. Nisi pariatur laborum cornhole kitsch tempor fingerstache Bushwick. </p> <div id='read-more' onclick="expand(this)"> READ MORE </div> </div> 

And I am trying to set that here: 我想在这里设置:

var toggleReadMore = function() {
    $('#read-more').click(function(e) {
        var prev = $(this).prev();            
        prev.css('height') = prev.scrollHeight + "px";
        $(this).hide();            
    });
};

What am I doing wrong? 我究竟做错了什么?

You cannot assign the value like that, you need to change it to something like 您不能像这样分配值,您需要将其更改为

prev.css('height', prev.scrollHeight)

Where the second argument accepts the scroll height. 第二个参数接受滚动高度的位置。 In case of multiple CSS properties, you can pass an object like 如果有多个CSS属性,则可以传递类似

prev.css({
  height: 'some height here',
  width: 'some width here'
});

** Change 3rd line like below ** **像下面一样更改第三行**

var toggleReadMore = function() {
$('#read-more').click(function(e) {
    var prev = $(this).prev();            
    prev.css('height', 50);
    $(this).hide();            
  });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM