-1

I have a code like below

var newWidth = Math.floor(width * .75) + 'px';
$(this).find("input:first").attr('style', 'width:newWidth ');

could you please correct me as what should be the right syntax as passing newWidth directly is not working also I have to do this via style only

4

5 回答 5

5

Use .css()

$(this).find("input:first").css('width', newWidth);
于 2013-08-14T13:13:08.203 回答
1

You can use the css method for this:

$(this).find("input:first").css('width', <your dynamic value> + 'px');

Here is a link to the documentation.

Otherwise, if you must use attr you should be able to do this:

$(this).find("input:first").attr('style', 'width:' + <your dynamic value> + 'px');
于 2013-08-14T13:14:00.383 回答
0

As mentioned in another answer, you can use .css():

var newWidth = Math.floor(width * .75) + 'px';
$(this).find("input:first").css({
    width: newWidth
});

Or you can use .width(), since you are using px units:

var newWidth = Math.floor(width * .75);
$(this).find("input:first").width(newWidth);
于 2013-08-14T13:42:22.213 回答
-2

You can try this .css("width","500px")

于 2013-08-14T13:16:53.120 回答
-2

Try this

$(this).find("input:first").attr('style', 'width:'+newWidth+'px');
于 2013-08-14T13:23:09.547 回答