如何在转发器中设置子 DIV 的文本?

发布于 2024-07-29 03:14:43 字数 645 浏览 2 评论 0原文

我有一个包含以下 HTML 片段的转发器:

<div id='parent'>
    <div id='child1'/>
    <div id='child2'>
         <script type="text/javascript" src="script.js"></script>
    </div>
</div>

我想使用 script.js 中的函数选择标记为“parent”的 div,并设置 ID 为“child1”的 div 的内部 HTML。

我的最终 html 应该如下所示:

<div id='parent'>
    <div id='child1'>This inner text was set by jQuery</div>
    <div id='child2'>
        <script type="text/javascript" src="script.js"></script>
    </div>
</div>

How do I do it using jQuery?

I have a repeater containing the following HTML snippet:

<div id='parent'>
    <div id='child1'/>
    <div id='child2'>
         <script type="text/javascript" src="script.js"></script>
    </div>
</div>

I would like to select the div tagged "parent" using a function inside script.js and set the inner HTML of the div with id "child1".

My final html should look like this:

<div id='parent'>
    <div id='child1'>This inner text was set by jQuery</div>
    <div id='child2'>
        <script type="text/javascript" src="script.js"></script>
    </div>
</div>

How do I do it using jQuery?

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

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

发布评论

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

评论(3

樱桃奶球 2024-08-05 03:14:43

首先,您不能在转发器中以这种方式编写 HTML 代码。 您不能将相同的 id 分配给页面上的多个元素。 如果您要识别它们,您的 HTML 将需要使用类来进行识别。

<div class="parent">
  <div class="child1"><div>
  <div class="child2">Click Me<div>
</div>

script 标签中的函数无法知道它们在 DOM 中相对于其他所有内容的位置。 您需要将函数调用绑定到某个元素,以便能够派生与其相关的 DOM 关系。 最简单的方法是使用 jQuery(或其他框架)向元素添加一些处理程序。

$(function() {
    $('.child2').click( function() {
        $(this).parent().find('.child1').html( 'some html' );
    });
});

上面的代码将为每个带有 child2 类的 DIV 添加一个点击处理程序,当它被点击时,会找到它的父 DIV(注意,该类在父级上是不必要的),然后是正确的子 DIV,其 HTML 为根据其类别进行更新。 另请注意,上述脚本只需添加到页面一次,而不是每个转发器项目一次。

First, you can't code your HTML this way in a repeater. You cannot assign the same ids to multiple elements on the page. Your HTML will need to use classes for identification, if you identify them at all.

<div class="parent">
  <div class="child1"><div>
  <div class="child2">Click Me<div>
</div>

Functions in the script tag have no way of knowing where they are in the DOM relative to everything else. You'll need to tie the function invocation to some element in order to be able to derive the relative DOM relationships to it. The easiest way is to use jQuery (or other framework) to add some handler to the elements.

$(function() {
    $('.child2').click( function() {
        $(this).parent().find('.child1').html( 'some html' );
    });
});

The above will add a click handler to each DIV with class child2 and when it is clicked, will find it's parent DIV (note that the class is unnecessary on the parent) and then the correct child whose HTML to update based on its class. Also, note that the above script only needs to be added to the page once, not once per repeater item.

空名 2024-08-05 03:14:43
$('#parent').children('#child1').html('html goes here');
$('#parent').children('#child1').html('html goes here');
娇纵 2024-08-05 03:14:43

你的问题没有多大意义。 您可以这样做:

$("#child1").html("blah");

您不需要对 #parentthis 的引用。

但假设情况并非如此,您可以通过多种方式找到与 this 相关的元素:

$(this).children("#child1")

$(this).find("#child1") // descendant, not just child

$("#child1", this) // descendant, not just child

Your question doesn't make a lot of sense. You can just do this:

$("#child1").html("blah");

You don't need a reference to #parent or this.

But assuming that wasn't the case, there are multiple ways you can find an element relative to this:

$(this).children("#child1")

or

$(this).find("#child1") // descendant, not just child

or

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