English 中文(简体)
用JavaScript写入到非唯一div
原标题:Writing to a non-unique div with Javascript

我正在为学校建立一个多道进食 RSS 阅读器。

aList是涵盖每个个体Feed的div(Feed的数量会波动)。

标题是被当前订阅源属性填充的 div。此外,如果被单击,它将把当前订阅源的属性列表加载到内容中。

我不禁要问,当提里特被点击时,我如何能把属性活化地装入大陆,因为那将是非独一无二的。

预先感谢您的帮助

安德鲁 (Ān dé lǔ)

问题回答

document.getElementsByClassName(aList).getElementsByTagName(div) document.getElementsByClassName(aList).getElementsByTagName(div)

你应该研究一下jQuery选择器,以便进行DOM操作等。就像... (Note: Translation may vary based on context)

$("div.theContent").attr("name", "value");

通过使用jQuery,您可以使用以下代码:

 $(".theTitle").bind("click", function(){
    $el = $(this);
           $el.parent().$(".theContent").load( ajax/content.php?news=  . $el.text());
 });

这将使得您所有的链接都可以被点击,在点击后,更新它们对应的内容

,其值为 ajax/content.php?news=theTitle-value。

使用一个好的Javascript库,比如PrototypejQuery。现在可能看似琐碎,但长远来看这些框架可以节省你大量的时间

在这两个框架中,你可以选择:

$( div.theTitle )

用jQuery,你可以做到:

$( div.theTitle ).click( function() {
    var title = $(this).text();
    var contentDiv = $(this).siblings( div.theContent );
    // Do something with contentDiv and the title
} );

这将使得每个 theTitle div 都有一个 onClick 事件,它会对其关联的 theContent div 进行某种处理。

<div class="aList">
<div class="theTitle" onclick="fillContentBox(this)"></div>
<div class="theContent"></div>
</div>

页: 1

function fillContentBox(div) {
  var theContentDiv = div.parentNode.getElementsByTagName("div")[1];
  // statements that do things with theContentDiv
}

如果您不想更新超过一个元素,您必须能够确定要更新的元素。如果元素被分组在拥有“id”值的其他东西内,您可以利用这一点。 Translated back into English: If you don't want to update more than one element, you have to be able to determine which element you want to update. If the elements are grouped inside something else that has an "id" value, you can take advantage of that.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!