简体   繁体   English

棘手的javascript / jquery / YUI问题,用于控制之间的空白 <input> 元素

[英]Tricky javascript/jquery/YUI issue for controlling white space between <input> elements

This one is a bit complicated, and I will describe my initial problem in detail and where I am at the moment in solving it. 这有点复杂,我将详细描述我的最初问题以及目前解决该问题的位置。

The problem: 问题:

My CMS outputs SOME of its HTML for inputs as follows: 我的CMS输出一些HTML作为输入,如下所示:

<input type="text"><input type="submit">

Now because these input elements are not each on their own lines, there is a lack of white space between the button and the textfield like so: 现在,由于这些输入元素不在各自的行上,因此按钮和文本字段之间缺少空白,如下所示:

问题-没有空间

The preferred way, and the majority of the HTML generated is in fact like this, is to have the HTML on seperate lines: 首选的方法(实际上生成的大多数HTML都是这样)是将HTML放在单独的行上:

<input type="text"/>
<input type="submit"/>

so that they render like this: 这样它们就这样渲染:

应该是这样的

Now, I cannot alter the HTML (generated by CMS), and I cannot play around with margins and negative margins as they will affect all inputs. 现在,我无法更改HTML(由CMS生成),并且无法使用边距和负边距,因为它们会影响所有输入。

I have found this jQuery solution that seems to work PERFECTLY and fixes the problem: 我发现此jQuery解决方案似乎可以正常工作并解决了问题:

$('input').after(" ");

It gives consistent white space between all input elements as if they were written in the correct way. 它在所有输入元素之间提供一致的空格,就好像它们是以正确的方式编写的一样。 BUT, I cannot use jQuery in my solution. 但是,我无法在解决方案中使用jQuery。 We do not work with the jQuery library. 我们不使用jQuery库。 I can only use pure javascript, or YUI3. 我只能使用纯JavaScript或YUI3。

Now, someone on stackoverflow has helped me to convert the above jQuery into the following YUI3 code: 现在,stackoverflow上的某人帮助我将上述jQuery转换为以下YUI3代码:

Y.all('input').insert('&nbsp','after');

and this does work in that it adds the white space where there was none. 这样做的确起作用,因为它会添加没有空白的空白。 BUT, the problem with this piece of code is that it also adds an extra whitespace to the correct pieces of HTML which already has whitespace. 但是,这段代码的问题在于,它还会为正确的HTML部分(已经具有空白)添加一个额外的空白。

Now my question is: 现在我的问题是:

Is there a way to 有没有办法

(1) change (1)变更

Y.all('input').insert('&nbsp','after');

so that it functions exactly the same way as 因此它的功能与

$('input').after(" ");

or, alternatively (2) is there perhaps a pure javascript solution that will do what the jQuery solution do, 或者,或者(2)也许有一个纯javascript解决方案可以完成jQuery解决方案的工作,

or perhaps (3) is there a way where I can first remove ALL whitespace between elements, and then add a single white space with the YUI method above? 也许(3)有没有一种方法,我可以先删除元素之间的所有空格,然后使用上面的YUI方法添加单个空格?

Thanks, and apologies for the vague question title! 谢谢,对于模糊的问题标题表示歉意! :) :)

EDIT: Just a little extra info. 编辑:只是一些额外的信息。

Here is the problem in action: http://jsfiddle.net/C3sHf/3/ 这是实际的问题: http : //jsfiddle.net/C3sHf/3/

Here you can see how the jQuery code fixes the problem and makes consistent white space: http://jsfiddle.net/C3sHf/2/ 在这里,您可以看到jQuery代码如何解决问题并保持一致的空格: http : //jsfiddle.net/C3sHf/2/

and here is the YUI3 solution but it doesnt work as desired: http://jsfiddle.net/9eTrP/1/ 这是YUI3解决方案,但无法按预期工作: http : //jsfiddle.net/9eTrP/1/

CSS should be the proper way of formatting things. CSS应该是格式化事物的正确方法。

input[type="submit"]{
    margin-left: 0.5em;
}

or 要么

input[type="text"]+input[type="submit"]{
    margin-left: 1em;
}

http://jsfiddle.net/5eG4y/ http://jsfiddle.net/5eG4y/


You can create a text-node and insertBefore the submit using plain javascript DOM 您可以使用纯javascript DOM创建文本节点并在提交之前insertBefore

var submit = document.querySelector('input[type="submit"]');
var foo = document.createTextNode("Foo");

submit.parentElement.insertBefore(foo, submit);

http://jsfiddle.net/5eG4y/1/ http://jsfiddle.net/5eG4y/1/

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

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