Hướng dẫn get element by id php - lấy phần tử bằng id php

(Php 5, Php 7, Php 8)

DomDocument :: GetEuityByID - Tìm kiếm một phần tử có ID nhất địnhSearches for an element with a certain id

Thông số

elementId

Giá trị ID duy nhất cho một phần tử.

Trả về giá trị

Trả về Domelement hoặc null nếu phần tử không được tìm thấy.DOMElement or null if the element is not found.

Ví dụ

Ví dụ #1 DomDocument :: GetEuityById () Ví dụ

Các ví dụ sau sử dụng Book.xml có chứa những điều sau:


  
  
  
  
  
  
  
  
]>


  
    PHP Basics
    Jim Smith
    Jane Smith
    PHP Basics provides an introduction to PHP.

]]>
PHP Advanced Programming Jon Doe

$doc

= new DomDocument;// We need to validate our document before referring to the id
$doc->validateOnParse true;
$doc->Load('book.xml');

echo

"The element whose id is 'php-basics' is: " $doc->getElementById('php-basics')->tagName "\n";?>

Ví dụ trên sẽ xuất ra:

The element whose id is 'php-basics' is: book 

Xem thêm

  • DomDocument ::

ed at edgiardina dot com ¶

12 năm trước

Please note that if your HTML does not contain a doctype declaration, then getElementById will always return null.

Carl2088 tại Gmail Dot Com ¶

13 năm trước

From my experience, getElementById seem to work fine without any setups if you have loaded a HTML document. But in order for getElementById to work with a simple XML document that you've "constructed", you have to set up the id with "xml:" prefix and use setIdAttribute on the element you created or it won't work. See following example, hope this will save someone's frustration. If you have loaded the xml file, then all you have to make sure is the ID has a xml: prefix for the attribute.  But if you start to append the XML document, don't forget to setIdAttribute on the id name or those elements or getElementById will return null when you try to find them.

/* test.xml
   
   
       
            Some Data
       

   

    */
$xmlDom = new DOMDocument('1.0', 'utf-8');
   
$xmlDom->formatOutput = true;                        // we want a nice output

The element whose id is 'php-basics' is: book 
0

The element whose id is 'php-basics' is: book 
1

Paradox_haze tại Live Dot de ¶

12 năm trước

The element whose id is 'php-basics' is: book 
2

The element whose id is 'php-basics' is: book 
3

The element whose id is 'php-basics' is: book 
4

Carl2088 tại Gmail Dot Com ¶

13 năm trước

The element whose id is 'php-basics' is: book 
5

The element whose id is 'php-basics' is: book 
6

The element whose id is 'php-basics' is: book 
7

The element whose id is 'php-basics' is: book 
8

The element whose id is 'php-basics' is: book 
9

The element whose id is 'php-basics' is: book 
4

Paradox_haze tại Live Dot de ¶

Simon ở một nơi nào đó dot com ¶

elementId1

elementId2

elementId3

elementId4

elementId5

elementId6

The element whose id is 'php-basics' is: book 
4

15 năm trước

Hoi ¶

elementId8

elementId9

null0

The element whose id is 'php-basics' is: book 
4

2 năm trước

Ẩn danh ¶

null2

16 năm trước

Hoi ¶

null3

2 năm trước

13 năm trước

null4

Paradox_haze tại Live Dot de ¶

Simon ở một nơi nào đó dot com ¶

null5

null6

null7

The element whose id is 'php-basics' is: book 
4

Nhận được phần tử bằng ID làm gì?

Phương thức tài liệu getEuityById () trả về một đối tượng phần tử biểu thị phần tử có thuộc tính ID khớp với chuỗi được chỉ định. Vì ID phần tử được yêu cầu là duy nhất nếu được chỉ định, chúng là một cách hữu ích để nhanh chóng truy cập vào một phần tử cụ thể.returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

Làm thế nào có thể nhận được giá trị phần tử HTML trong PHP?

Sử dụng $ _POST hoặc $ _GET SuperGlobals của PHP để truy xuất giá trị của thẻ đầu vào thông qua tên của thẻ HTML..

Tại sao getEuityByid null?

getEuityById (...) là null dường như chỉ ra rằng không có yếu tố nào như vậy với ID được truyền cho getEuityById () tồn tại.Điều này có thể xảy ra nếu mã JavaScript được thực thi trước khi trang được tải đầy đủ, vì vậy nó không thể tìm thấy phần tử.there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element.

Làm cách nào để nhấp vào một tài liệu trong getEuityByid?

Tài liệu GetEuityByID Các sự kiện onclick có thể được thêm vào bằng phương thức addEventListener () hoặc thuộc tính onclick.Với phương thức addEventListener (), bạn có thể thêm nhiều sự kiện onclick vào một phần tử nhưng với thuộc tính onclick, bạn chỉ có thể thêm 1 sự kiện onclick vào một phần tử tại một thời điểm.using the addEventListener() method or onclick property. With addEventListener() method, you can add multiple onclick events on an element but with onclick property, you can only add 1 onclick event on an element at a time.