[英]How to set the css property of dynamic data
I am using javascript to write some content in a div tag. 我正在使用javascript在div标签中写一些内容。 The data is received through ajax.
数据通过ajax接收。 Here is the script
这是脚本
$.ajax({
url :' the url',
data : {
productValue: selectedText,
giveMyId : saveMyName
},
type: 'POST',
dataType: 'json',
success: function(data)
{
var imageHTML='';
var dataLength=data.length;
photosHasPath=data[0];
if(dataLength > 1)
{
for(var i=1;i<dataLength;i++)
{
allProductNames.push(data[i]);
imageHTML += '<img src="' + photosHasPath + data[i] + '" height="100" width="100" style="padding: 0.5em;">' +
' <input type=\"button\" class=\"deletePhoto\" value=\"Delete\" name=\"'+data[i]+'\">';
}
$('#pastePhotos').html(imageHTML);
}
else
{
$('#pastePhotos').html('<i>No photos left.</i>');
}
}
});
Now I am trying to set the css property of this div (having id 'pastePhotos') which is display:inline-table . 现在,我试图设置此div的css属性(具有id'pastePhotos'),该属性为display:inline-table 。 But its not working.
但是它不起作用。 Its showing the images and the delete button like a text in a single line.
它像一行文字一样显示图像和删除按钮。
Question:- 题:-
SO how can i set the css property for it? 所以,我该如何设置css属性呢?
Note:- 注意:-
I have already tried the following things 我已经尝试了以下方法
1 . 1。
imageHTML += '<img src="' + photosHasPath + data[i] + '" height="100" width="100" style="padding: 0.5em; display: inline-table;">' +
' <input type=\"button\" class=\"deletePhoto\" value=\"Delete\" name=\"'+data[i]+'\">';
2 . 2。
'<div style="display: inline-table;">' + imageHtml + '</div>'
3 . 3。
<div id="pastePhotos" style="display: inline-table;">
There is no need to put the styles inline in the Javascript code. 无需将样式放在Javascript代码中。 Since you have already given an
id
, you can simply add the styles in a CSS file or style
element 由于已经给定了
id
,因此您可以简单地将样式添加到CSS文件或style
元素中
#pastePhotos {
display: inline-table;
}
However, the main problem seems to be missing style for the img
and input
elements. 但是,主要问题似乎是
img
和input
元素缺少样式。 For this, you must add a 为此,您必须添加一个
#pastePhotos img, #pastePhotos input {
display: table-cell;
}
and wrap them in an additional div
for the table-row
并将它们包装在
table-row
的另一个div
中
imageHTML += '<div class="img-row"><img src="' + photosHasPath + data[i] + '" height="100" width="100" style="padding: 0.5em;">' +
' <input type=\"button\" class=\"deletePhoto\" value=\"Delete\" name=\"'+data[i]+'\"></div>';
#pastePhotos .img-row {
display: table-row;
}
You can either use jquery to directly add css attributes 您可以使用jquery直接添加CSS属性
As in $('#pastePhotos').css (propertyName, value )
如
$('#pastePhotos').css (propertyName, value )
http://api.jquery.com/css/#css2 http://api.jquery.com/css/#css2
or if you have your attributes as a style class you can also use jquery to add this class 或者,如果您具有样式类的属性,则也可以使用jquery添加此类
$('#pastePhotos').addClass( "myStyle" );
Add a complete: function(){}
block in the $.ajax({})
. 在
$.ajax({})
添加一个complete: function(){}
块。 Then add css using javascript by $('#pastePhotos').css("display", 'inline-table')
and so on with in that complete
block. 然后,添加使用JavaScript的CSS
$('#pastePhotos').css("display", 'inline-table')
等与该complete
块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.