简体   繁体   English

通过JS / jQuery访问和操纵DOM中的子元素

[英]Accessing and manipulating child elements in DOM by JS/jQuery

I've got a problem in accessing my elements upon page loading. 在页面加载时访问元素时出现问题。

I have simplified my code down to this but to no luck! 我已经简化了我的代码,但是没有运气!

<div id="NavigationButtons">
  <p id="pagenum">
    10
  </p>
</div>

JS: JS:

function abi(){
  var page = document.getElementById("NavigationButtons").getElementById("pagenum");
}

I've tried different JSFiddle options like onload , ondomready , no wrap head, no wrap body... I've also tried to write this in jQuery: 我尝试了不同的JSFiddle选项,例如onloadondomready ,没有换行头,没有换行主体...我也试过用jQuery编写:

var p = ('#NavigationButtons > p#pagenum').text();

I am so mixed up between JS and jQuery.This is getting annoying. 我对JS和jQuery感到很困惑,这很烦人。

As I was saying in my comment, you are missing the all-important $ on your jQuery version: 就像我在评论中说的那样,您缺少jQuery版本上最重要的$:

Basic test of selector: 选择器的基本测试:

$(function(){
    var p = $('#NavigationButtons > p#pagenum').text();
    alert(text);
});

but you really need to hook up your button code from jQuery too: 但您确实也需要从jQuery连接您的按钮代码:

$(function(){
    $('#abi').click(function() {
            var p = $('#NavigationButtons > p#pagenum').text();
            alert(p);
        });
});

and give your button an id: 并给你的按钮一个ID:

<input id="abi" type="button"/>`

note: $(function(){ YOUR CODE HERE }); 注意: $(function(){ YOUR CODE HERE }); is a shortcut for $(document).ready(function(){ YOUR CODE HERE }); $(document).ready(function(){ YOUR CODE HERE });的快捷方式$(document).ready(function(){ YOUR CODE HERE });

http://jsfiddle.net/TrueBlueAussie/wK2FM/3/ http://jsfiddle.net/TrueBlueAussie/wK2FM/3/

*If you are going to use jQuery at all I would recommend forgetting that getElementById exists :)* *如果您将要全部使用jQuery 我建议您忘记存在getElementById :)*

Update: 更新:

As your item of interest already has an id you can simplify to: 由于您感兴趣的商品已经具有ID,因此您可以简化为:

$(function(){
    $('#abi').click(function() {
            var p = $('#pagenum').text();
            alert(p);
        });
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/wK2FM/4/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/wK2FM/4/

Notes about JavaScript version: 有关JavaScript版本的注意事项:

Javascript does not chain methods together the way jQuery does. Javascript不像jQuery那样将方法链接在一起。 document.getElementById("NavigationButtons").getElementById("pagenum") will never work. document.getElementById("NavigationButtons").getElementById("pagenum")将永远无法使用。 getElementById only exists on document, so trying to then call it again, on the DOM element returned from the first call, will give an error. getElementById仅存在于文档中,因此在第一次调用返回的DOM元素上尝试再次调用它时,将产生错误。

As your final target element has an id you could just do: 由于您的最终目标元素具有ID,因此您可以执行以下操作:

var page = document.getElementById("pagenum");

Additions based on comment below: 根据以下评论添加内容:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/wK2FM/5/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/wK2FM/5/

 // Does not work as text is not a property of a DIV element
 var page=document.getElementById("pagenum").text;   

 // This one actually works as innerHTML IS a property of a DIV (and of all HTML elements)
 var page=document.getElementById("pagenum").innerHTML; 

 // Does not work as value is not a property of a DIV element
 var page=document.getElementById("pagenum").value;

if you want sensible sounding methods to access properties, stick with jQuery. 如果您想使用明智的探测方法来访问属性,请坚持使用jQuery。 If you have to use any jQuery on a page, you might as well do it all with jQuery (as the code will be generally more readable and shorter than the same in RAW JavaScript) 如果您必须在页面上使用任何 jQuery, 则最好也使用jQuery(因为与RAW JavaScript中的代码相比,该代码通常更具可读性且更短)

If you want to access an element with id #pagenum you can do so directly as follows: 如果要访问ID为#pagenum的元素, #pagenum可以直接进行以下操作:

 var page = document.getElementById("pagenum");

or using jQuery 或使用jQuery

$('#pagenum')

if you are using jquery you need to do very simply like this: 如果您使用的是jquery,则需要非常简单地执行以下操作:

HTML: HTML:

<input type="button" onclick="abi(this)"/>

Jquery: jQuery的:

function abi(event){

$("#NavigationButtons p#pagenum").text()

}

FIDDLE DEMO 现场演示

But i will suggest you to write jquery event this way: 但是我建议您这样编写jquery事件:

HTML: HTML:

<input type="button" id="btnAbi"/>

JQUERY: JQUERY:

$(document).ready(function(){

    $("#btnAbi").click(function(){ 

   $("#NavigationButtons p#pagenum").text();

    });

});

You can use following jQuery usind .find() : 您可以使用以下jQuery usind .find()

function abi(){
var page = $("#NavigationButtons").find("#pagenum").text();
}

You can use descendent (like below), but find is faster than it. 您可以使用后代(如下所示),但是查找比后代快。

jQuery with descendent selector : 带有后代选择器的jQuery:

function abi(){
    var page = $("#NavigationButtons #pagenum").text();
}

Result with find and descendent selector : Link 使用查找和后代选择器的结果: 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM