简体   繁体   English

使用 jquery 或 javascript 设置内联块 css

[英]set inline-block css with jquery or javascript

based on this example from @Bergi jsfiddle.net/CH9K8/1321/基于@Bergi jsfiddle.net/CH9K8/1321/的这个例子
the code below work fine:下面的代码工作正常:

#center{
display:inline-block;
}

but if I try this don't work in Chrome and Safari但是如果我尝试这个在 Chrome 和 Safari 中不起作用

$('#center').css('display','inline-block');

nor也不

document.getElementById('center').style.display='inline-block';

any solution?任何解决方案? thanks.谢谢。

EDIT ... ok now I understand why you say it work fine.编辑...好的现在我明白你为什么说它工作正常。 I have tried it in Chrome (v30) and safari v5.1 And not work ... but yes it work In Explorer, Firefox, and Opera.我已经在 Chrome (v30) 和 safari v5.1 中尝试过,但不起作用……但是在资源管理器、Firefox 和 Opera 中它可以工作。 So now the question is ... some solution for Chrome and Safari ?所以现在的问题是...... Chrome 和 Safari 的一些解决方案?

$('#center').css('display','inline-block');

正如 PHPglue 先前所述,这很好用,(当您实际将小提琴设置为使用 jquery 而不是 Mootools 时)正确的 div 不显示内联的问题是由于您同时使用了浮动和显示内联属性,他们彼此并不真正兼容。

$('#center').css('display', 'inline-block'); works if you're using jQuery.如果您使用 jQuery,则有效。 See: http://jsfiddle.net/CH9K8/1320/ .请参阅: http : //jsfiddle.net/CH9K8/1320/ Change it to block and back.将其更改为block并返回。

Pure JS code to show the use of block-inline : Pure JS代码展示block-inline的使用:

 withElem = document.querySelector('#withSpace') withoutElem = document.querySelector('#withoutSpace') btn1 = document.createElement('button') btn1.textContent = 'element1' btn2 = document.createElement('button') btn2.textContent = 'element2' btn3 = document.createElement('button') btn3.textContent = 'element1' btn4 = document.createElement('button') btn4.textContent = 'element2' spn = document.createElement('span') spn.style.display = 'inline-block' // Get a spacer of 20px: spn.style.width = '20px' //Append it to another node withElem.append(btn1, spn, btn2) withoutElem.append(btn3, btn4)
 <div id=withSpace></div><br> <div id=withoutSpace></div>

: spn.style.width = '20px' //Append it to another node parentElem.appendChild(spn) : spn.style.width = '20px' //追加到另一个节点 parentElem.appendChild(spn)

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

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