jquery 如何将两个带有选择器的变量传递给$()

hrysbysz  于 2023-05-17  发布在  jQuery
关注(0)|答案(1)|浏览(86)

有这样的代码:

$(".field--name-field-upload-artwork details table, .field--name-field-upload-artwork .js-form-type-managed-file").wrapAll('<div class="upload-file-wrapper"></div>');

但是我决定给变量添加选择器:

let $uploadedFilesTable = $(".field--name-field-upload-artwork details table");
 let $dragAndDrop = $(".field--name-field-upload-artwork .js-form-type-managed-file");

就像这样行不通:

$("${$uploadedFilesTable} + ',' + ${$dragAndDrop}").wrapAll('<div class="upload-file-wrapper"></div>');

就像这样行不通:

$($uploadFilesTable + ',' + $dragAndDrop).wrapAll('<div class="upload-file-wrapper"></div>');
tjrkku2a

tjrkku2a1#

下面是一个如何在选择器中使用多个变量的工作示例。检查下面的工作代码段。
希望这将解决您的查询。

$(document).ready(function(){
   let $uploadedFilesTable = $(".field--name-field-upload-artwork .details");
   let $dragAndDrop = $(".field--name-field-upload-artwork .js-form-type-managed-file");
   $($dragAndDrop).add($uploadedFilesTable).wrapAll('<div class="upload-file-wrapper">file wrapper</div>')
 })
.field--name-field-upload-artwork{
  background:#eaeaea;
  padding:10px;
}

.field--name-field-upload-artwork .details{
  background:blue;
  padding:10px;
}
.field--name-field-upload-artwork .js-form-type-managed-file{
  background:red;
  padding:10px;
}
.field--name-field-upload-artwork .upload-file-wrapper{
  background:yellow;
  padding:10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="field--name-field-upload-artwork">
main container
  <div class="details">
    Details
  </div>
  <div class="js-form-type-managed-file">
  js-form-type-managed-file
  </div>
</div>

相关问题