jquery在父div中选择div元素 [英] Jquery select div element inside parent div

查看:90
本文介绍了jquery在父div中选择div元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图选择另一个div内的div元素,并存储它的id或class。我试图做这个简单的函数来查看它是否工作:

  $('div')。on('click',函数(){
alert($(this).attr(id));
});

然而,每次我点击任何div,例如带idcontent输出的div时,因此,我的问题是如何更改该代码,以便它看起来在
divcontent-link2中。$ / b


 < div id =content-link2> 
< div class =logo>
< img class =imagesid =imagesrc =#alt =您的徽标>
< / div>

< div id =comments>
< form name =forma>
< textarea name =commentUserid =commentUserclass =refreshcols =40rows =5> Comments here ...
< / textarea>< BR>
< input type =submitvalue =Ready! ID = 发送数据 ><! - 的onClick = writeComment(E); - >
< div id =editTxtclass =refreshcontenteditable =true>
< p>此文本可以由用户使用。< / p>
< / div>
< / form>
< / div>
< / div>


解决方案

您需要稍微更改脚本



这是警告任何元素被点击的 id 的解决方案。 。如果它没有id,alert将显示 undefined



  $(document).on('click',function(e){alert($(e.target).attr(id));});  

< script src =https://ajax.googleapis.com/ajax/ libs / jquery / 2.1.1 / jquery.min.js>< / script>< div id =content-link2> < title>模板1< / title> < link href =http://localhost/fyproject/public/templates/css.css =stylesheettype =text / css> < div class =logo> < img class =imagesid =imagesrc =#alt =您的徽标> < / DIV> < div contenteditable =trueid =contentclass =draggable ui-widget-content refresh ui-draggable ui-draggable-handlestyle =position:relative;>< p> hlo< / p> ;< / DIV> < div id =comments> < form name =forma> < textarea name =commentUserid =commentUserclass =refreshcols =40rows =5>评论在这里...< / textarea>< br> < input type =submitvalue =Ready! ID = 发送数据 ><! - 的onClick = writeComment(E); - > < div id =editTxtclass =refreshcontenteditable =true> < p>此文本可以由用户使用。< / p> < / DIV> < /形式> < / div>< / div>

Hello people hope you can help me.

I am trying to select div elements that are inside another div and store either it's id or class. I tried to do this simple function to see if it works:

$('div').on('click', function(){
    alert($(this).attr("id"));
});

However everytime I click on any div for example div with id "content" output I get is "content-link2".

Therefore my question is how to change that code so it looks inside div "content-link2"

<div id="content-link2">
    <div class="logo">
        <img class="images" id="image" src="#" alt="Your Logo">
    </div>
    <div contenteditable="true" id="content" class="draggable ui-widget-content refresh ui-draggable ui-draggable-handle" style="position: relative;"><p>hlo</p></div>
    <div id="comments">
        <form name="forma">
            <textarea name="commentUser" id="commentUser" class="refresh" cols="40" rows="5">Comments here...
            </textarea><br>
            <input type="submit" value="Ready!" id="send-data"><!--onClick="writeComment(e);"--> 
            <div id="editTxt" class="refresh" contenteditable="true">
                <p>This text can be by the user.</p>
            </div>
        </form>
    </div>
</div>

解决方案

You need a little change your script.

Here is solution of alerting an id of any element was clicked. If it hasn't id, alert shows undefined

$(document).on('click', function(e){
    alert($(e.target).attr("id"));
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content-link2">
    <title>Template 1</title>
    <link href="http://localhost/fyproject/public/templates/css.css" rel="stylesheet" type="text/css">


    <div class="logo">
        <img class="images" id="image" src="#" alt="Your Logo">
    </div>
    <div contenteditable="true" id="content" class="draggable ui-widget-content refresh ui-draggable ui-draggable-handle" style="position: relative;"><p>hlo</p></div>
    <div id="comments">
        <form name="forma">
            <textarea name="commentUser" id="commentUser" class="refresh" cols="40" rows="5">Comments here...
            </textarea><br>
            <input type="submit" value="Ready!" id="send-data"><!--onClick="writeComment(e);"--> 
            <div id="editTxt" class="refresh" contenteditable="true">
                <p>This text can be by the user.</p>
            </div>
        </form>
    </div>
</div>

这篇关于jquery在父div中选择div元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆