Jscript-dan foydalanib Div'dan matnni oling

Div'dan matnni qanday olish mumkin?

Misol uchun :

<script>
    function cahnge() {
        alert(document.getElementById('udin'));
   }

</script>
<div class="ayah">
<div class="udin" onclick="cahnge()" >udin</div>
</div>

Div xabarini bosganda va xabar divning matni bo'lganida ogohlantirish xabarini qabul qilmoqchiman.

Rahmat.

0
matnni olish uchun document.getElementById ('udin') ni kiriting va uni ishlatish uchun id = 'udin' ni qo'shing.
qo'shib qo'ydi muallif Deep 3015, manba

8 javoblar

Siz yaqinsiz.

getElementById is checking that the div has an ID, but your div is a class. You also need to add .textContent to get the actual content and not just the div object.

<script>
    function cahnge() {
        alert(document.getElementById('udin').textContent);
   }

</script>
<div class="ayah">
<div id="udin" onclick="cahnge()" >udin</div>
</div>
2
qo'shib qo'ydi

Buni onclick işleyicisindeki argument sifatida qabul qiling

<div class="ayah">
   <div class="udin" onclick="cahnge(this)" >udin</div>
</div>

Javascriptda

function cahnge(obj) {
    alert(obj.innerText); 
    //alert(obj.innerHTML); 
}

Agar div boshqa elementlardan iborat bo'lsa, unda 'innerHTML' foydalaning. "InnerText" dan foydalanmasangiz.

1
qo'shib qo'ydi

Agar siz jQuery dan foydalansangiz, bu juda oson .text() ni ishlatish - bu elementni aniq maqsadga yo'naltirish uchun identifikatordan foydalaning, siz ko'plab o'xshash bo'lishi mumkin bo'lgan sinfdan foydalanmoqdasiz.

$(document).ready(function(){
   $("#target").click(function(){
    $("#target").text("Hello world!");
   });
});


 <div class="ayah">
 <div id="target" class="udin">udin</div>
 </div>
0
qo'shib qo'ydi

Bir harakat qilib ko'ring

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

<html>

<body>
  <div class="ayah">
    <div class="udin">udin</div>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    $(function() {
      $elem = $('.udin');
      $elem.on('click', function() {
        var msg = $elem.html();
        alert(msg);
      });
    });
  </script>
</body>

</html>
</div> </div>
0
qo'shib qo'ydi
<script>
    function change(aObject) {
        alert(aObject.innerHTML);
    }    
</script>

<div class="ayah">
<div class="udin" onclick="change(this);">udin</div>
</div>
0
qo'shib qo'ydi
Ushbu kod bloki OP savoliga javob berishi mumkin bo'lsa-da, bu kodning ushbu kodning savolning kodidan qanday farq qilganligini, nima o'zgartirganingizni, nima uchun uni o'zgartirganingizni va nima uchun bu muammoni hal qilishini tushunsangiz, bu javob juda foydali bo'ladi boshqalarni tanishtirmasdan.
qo'shib qo'ydi muallif Mistalis, manba

getElementByClassName dan foydalaning:

<script>
    function cahnge() {
        alert(document.getElementsByClassName('udin')[0].innerHTML);
   }

</script>
<div class="ayah">
<div class="udin" onclick="cahnge()" >udin</div>
</div>

Bu erda JSFiddle

0
qo'shib qo'ydi
matnni olish uchun document.getElementByClassName ("udin") [0] .innerHTML dan foydalaning.
qo'shib qo'ydi muallif Mihai Alexandru-Ionut, manba

Buni ko'ring:

<script>
    function change(aObject) {
        alert(aObject.textContent);
   }

</script>
<div class="ayah">
<div class="udin" onclick="change(this);">udin</div>
</div>
0
qo'shib qo'ydi
Sizning echimingiz uchun sizga janob Georgi. Bu ish.
qo'shib qo'ydi muallif Timothy Varian, manba

Birinchidan, siz id emas, balki sinf ni tanladingiz. Shunday qilib, ushbu o'zgarishlarni HTML kodida kiriting:

<div class="ayah">
<div id="udin" onclick="cahnge()" >udin</div>
</div>

Javascriptda:

function cahnge() {
    alert(document.getElementById("udin").innerHTML);

}

InnerHTML matnni olishingizga yordam beradi.

0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb