简体   繁体   English

如何使用jquery和javascript更改div的内容?

[英]How can I change the content of a div using jquery and javascript?

I am pretty new to javascript and jquery. 我对javascript和jquery很陌生。 I am trying to rotate the contents of 2 div's located within a wrapper div. 我正在尝试旋转位于包装div内的2 div的内容。 One div contains a photo and the other some styled text (h1, h3, p, span). 一个div包含一张照片,另一个包含一些样式化的文本(h1,h3,p,span)。 It is a gallery of 4 surf boards and there are 4 photos and 4 descriptions. 这是一个包含4个冲浪板的画廊,并且有4张照片和4个描述。 I have been trying to rotate the contents of the div's when the links are clicked. 单击链接时,我一直在尝试旋转div的内容。 The div that contains the photos rotates fine. 包含照片的div旋转正常。 I can get one description in the text div(currentBoard) to rotate but after that the only thing that will change are the photos and the description stays static on the first one that was clicked. 我可以在文本div(currentBoard)中获得一个描述以进行旋转,但是此后唯一会改变的是照片,并且描述在第一个被单击的对象上保持不变。 Since the div containing the text has a class already applied I am using the id to rotate the contents. 由于包含文本的div已经应用了一个类,因此我使用id来旋转内容。 The actual contents for this div are in the css using :after and :before and as the id's change for this div I am trying to apply the contents to be that of the new id. 这个div的实际内容是在CSS中使用:after和:before,并且作为该div的ID的更改,我尝试将内容应用为新ID的内容。 I just cannot get the text contents div to rotate in sync with the photos div. 我只是无法使文本内容div与照片div同步旋转。 The code below is what I have now and I have tried many things already. 下面的代码是我现在拥有的,并且我已经尝试了很多东西。 currentBoard is the id in the text div I would like to change. currentBoard是我要更改的文本div中的ID。 I am assuming I need to somehow remove the old id first then add in the new one but everything I have tried has failed. 我假设我需要以某种方式先删除旧ID,然后再添加新ID,但是我尝试过的所有操作均失败了。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks! 谢谢!

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(evt) {
$("a:has(img.boardGallery)").click(function() {
var largePath = $(this).attr("href");
var caption = $(this).attr("id");
var currentBoard = $("#currentBoard");    
currentBoard.attr({ id: caption });
$("#photo_large").attr({ src: largePath });
return false;       
}); 
}); 
</script>

Switching an ID will not change the content of a div. 切换ID不会更改div的内容。
If you want to change what is displayed in a div, you should just write a new value into the inner html. 如果要更改div中显示的内容,则只需将一个新值写入内部html。

Here is a link to a question showing how that is done: How to replace innerHTML of a div using jQuery? 这是显示问题的链接,该问题如何完成的: 如何使用jQuery替换div的innerHTML?

If you want to have one div disappear and another show up, you should do this with CSS properties and change the 'display' property. 如果要使一个div消失而另一个显示,则应使用CSS属性并更改“ display”属性。 You can do this with Show() and Hide() in jQuery. 您可以使用jQuery中的Show()和Hide()来实现。 Here is a link to a question showing how to do this: jquery show/hide div 这是一个显示该操作方法的问题的链接: jquery show / hide div

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

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