javascript - 有没有一种简洁的方法可以在同一函数中更改多个 div 的innerHTML?

标签 javascript jquery

当单击按钮时,我将使用innerHTML 替换多个div 的内容。

我目前在单击按钮时加载了一个新的视频源,但还希望在同一按钮单击时更改其他 div 中的几种其他类型的内容。

首先,我不确定一个接一个地编写这些函数(如我包含的代码片段中所示)是否能够正确执行所有三个函数,其次我希望它们都可以在同一个函数中以某种方式简化编码。

我四处寻找如何更改一个函数中的多个 HTML 元素,但我能找到的大多数 innerHTML 信息似乎只解决了一个元素的更改。

//I currently have this:
$('#dog-video').on('click', function() {
  document.getElementById("video-content").innerHTML = '<source src= "dogvideo.mp4" type="video/mp4">';
video-content.load();
})

// But would also like to do both of these simultaneously
$('#dogvideo').on('click', function() {
  document.getElementById("h2-content").innerHTML = 'Dog Video Heading';
})

$('#dogvideo').on('click', function() {
  document.getElementById("p-content").innerHTML = 'This is dog text';
})

最佳答案

将所有三个 DOM 更新放在同一个函数体内,或者在该函数中使用 jquery 都没有问题。

$('#dog-video').on('click', function() {
  $('#video-content').html('<source src= "dogvideo.mp4" type="video/mp4">');
  $('#h2-content').html('Dog Video Heading');
  $('#p-content').html('This is dog text');
  $('#video-content').load();
});

关于javascript - 有没有一种简洁的方法可以在同一函数中更改多个 div 的innerHTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58359992/

相关文章:

javascript - scope.$observe 不是 AngularJS 指令中的函数

javascript - 保存对 angularjs 中对象的响应

javascript - 以字节流形式下载 PDF

javascript - 如何让图像转到 div 的位置?

javascript - 我可以做些什么来优化我的 WebGL 动画?

php - Javascript 确认删除

javascript - 查找并替换 URL 中的特殊字符 - jQuery

c# - AJAX 调用后如何重定向到 "Error" View 页面?

jquery - 使用 jquery 如何根据从另一个下拉字段中选择的值过滤下拉字段

javascript - HTML 动态/响应元素定位