0
اضافه کردن data-toggle به تگ div بکمک jquery
سلام
من کد زیر را دارم
<div id="@collapse" class="collapse show" >
<p class="border-top p-3">
Anim pariatur cliche reprehenderit,
enim eiusmod high life accusamus
terry richardson ad squid. 3 wolf moon officia aute, non cupidatat
skateboard dolor brunch.
Food truck quinoa nesciunt laborum eiusmod.
Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin
coffee nulla assumenda shoreditch et.
Nihil anim keffiyeh helvetica,
craft beer labore wes anderson cred nesciunt sapiente ea proident.
</p>
</div>
حال میخواهم بکمک js یا jq بعد اجرا شدن برنامه به div که دارای کلاس collapse show
<div id="@collapse" class="collapse show" >
کد زیر اضافه شود چکار باید کنم
data-parent="#accordion"
باتشکر
2 پاسخ
1
سلام ننوشتین با چه رویدادی میخواین اون کد اضافه بشه من با رویداد کلیک روی یه دکمه کد میذارم.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h1>Hello World</h1>
<p>Click the button to add a class attribute with the value of "democlass" to the h1 element.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementsByTagName("H1")[0].setAttribute("data-parent", "#accordion");
}
</script>
</body>
</html>
شما می تونید به جای استفاده از این getElementsByTagName از این گزینه استفاده کنید که براساس ایدی مورد نظرت انتخاب انجام بده getElementById
1
سلام، وقت بخیر
اگر از jquery استفاده می کنید می تونید از اکشن attr استفاده کنید:
$(function(){
$('div.collapse.show').attr('data-parent','#accordion');
});