如何使用 YUI 按名称查找输入并在其后插入内容

发布于 2024-07-21 06:43:11 字数 383 浏览 5 评论 0原文

如何使用 YUI 按名称定位文本输入并在其后插入 div?

例如,这不起作用:

<input name="some_name" type="text">

<script>
var el = new YAHOO.util.Element(document.createElement('div'));
var some_element = document.getElementsByName('some_name');

// doesn't work .... replacing 'some_element' with 'document.body' works
el.appendTo(some_element);

</script>

How can I use YUI to locate a text input by name and insert a div after it?

For example, this doesn't work:

<input name="some_name" type="text">

<script>
var el = new YAHOO.util.Element(document.createElement('div'));
var some_element = document.getElementsByName('some_name');

// doesn't work .... replacing 'some_element' with 'document.body' works
el.appendTo(some_element);

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一身仙ぐ女味 2024-07-28 06:43:11

如前所述,document.getElementsByName 返回一个数组。 您可能希望为输入提供一个与 name 属性同名的 ID。 (旁白:这对于表单来说是常见且良好的做法。当您执行此操作时,其他 js 库提供了有用的扩展。)

<input name="some_name" id="some_name" type="text">
<script>
(function () {
var el = new YAHOO.util.Element(document.createElement('div'));
// var some_element = document.getElementByName('some_name')[0];
var some_element = document.getElementsById('some_name'); // preferred, faster
// el.appendTo(some_element);
YAHOO.util.Dom.insertAfter(el,some_element);
})();
</script>

还要注意 insertAfter 而不是 appendTo 的使用。 您不希望 el 成为输入元素的子元素。 你希望它成为下一个兄弟姐妹。 输入没有子项。 最后,您将这些变量添加到全局命名空间中。 这可能是问题,也可能不是问题,但将代码包装在匿名函数中通常是个好主意,除非您打算让变量具有全局作用域并稍后重用它们,但随后您可能想为它们提供适当的命名空间。

希望有帮助(并且没有太多信息。);)

As mentioned, document.getElementsByName returns an array. You may want to give your input an ID with the same name as the name attribute. (Aside: This is common and good practice for forms. Other js libraries provide helpful extensions when you do this.)

<input name="some_name" id="some_name" type="text">
<script>
(function () {
var el = new YAHOO.util.Element(document.createElement('div'));
// var some_element = document.getElementByName('some_name')[0];
var some_element = document.getElementsById('some_name'); // preferred, faster
// el.appendTo(some_element);
YAHOO.util.Dom.insertAfter(el,some_element);
})();
</script>

Also notice the use of insertAfter rather than appendTo. You do not want el to be a child of your input element. You want it to be the next sibling. Inputs do not have children. Also lastly, you're adding these variables to the global namespace. This may or may not be a problem, but it's generally a good idea to wrap your code in an anonymous function unless you intend for the variables to have global scope and reuse them later, but then you might want to provide a proper namespace for them.

Hope that helps (and not too much info.) ;)

装纯掩盖桑 2024-07-28 06:43:11

document.getElementsByName('some_name') 总是返回一个集合(一个数组),如果您确定该名称是唯一的,您可以安全地编写它。

var some_element = document.getElementsByName('some_name')[0];

document.getElementsByName('some_name') always return a collection (an Array), if you are sure that the name is unique you can safely write this.

var some_element = document.getElementsByName('some_name')[0];
烟花易冷人易散 2024-07-28 06:43:11

使用 YUI 3,您现在可以执行此操作

<script src="http://yui.yahooapis.com/3.18.1/build/yui/yui-min.js"></script>

<input id="some_id" type="text">

<script>
  YUI().use('node', function(Y) {
    var contentNode = Y.Node.create('<p>');
    contentNode.setHTML('This is a para created by YUI...');
    Y.one('#some_id').insert(contentNode, 'after');
  });
</script>

但请记住YUI 即将停产

With YUI 3 you can now do this:

<script src="http://yui.yahooapis.com/3.18.1/build/yui/yui-min.js"></script>

<input id="some_id" type="text">

<script>
  YUI().use('node', function(Y) {
    var contentNode = Y.Node.create('<p>');
    contentNode.setHTML('This is a para created by YUI...');
    Y.one('#some_id').insert(contentNode, 'after');
  });
</script>

But keep in mind YUI is being discontinued!

忘你却要生生世世 2024-07-28 06:43:11

您基本上会设置一个条件并说 name="some_name"。
请参阅下面的代码并观察是否仍然插入了

段落。

<script src="http://yui.yahooapis.com/3.18.1/build/yui/yui-min.js"></script>

<input id="some_id" type="text" name="inpuname" >

<script>
  YUI().use('node', function(Y) {
    var contentNode = Y.Node.create('<p>');
    contentNode.setHTML('Paragraph created by YUI by searching for *INPUNAME*...');
    Y.one('input[name="inpuname"]').insert(contentNode, 'after');
  });
</script>

You basically put a condition and say name="some_name".
See the code below and observe that is still does insert the

aragraph.

<script src="http://yui.yahooapis.com/3.18.1/build/yui/yui-min.js"></script>

<input id="some_id" type="text" name="inpuname" >

<script>
  YUI().use('node', function(Y) {
    var contentNode = Y.Node.create('<p>');
    contentNode.setHTML('Paragraph created by YUI by searching for *INPUNAME*...');
    Y.one('input[name="inpuname"]').insert(contentNode, 'after');
  });
</script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文