Hướng dẫn php get element by id from html - php lấy phần tử theo id từ html

(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

Hoi ¶

null4

2 năm trước

Ẩn danh ¶

null5

null6

null7

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

16 năm trước

Document.getEuityById () Phương thức tài liệu getEuityById () trả về một đối tượng phần tử đại diện cho 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ể. The Document method getElementById() 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 cách nào để tìm phần tử theo giá trị ID?

Phương thức getEuityById () trả về một phần tử có giá trị được chỉ định.Phương thức getEuityById () trả về null nếu phần tử không tồn tại.Phương thức getEuityById () là một trong những phương pháp phổ biến nhất trong HTML DOM.Nó được sử dụng gần như mỗi khi bạn muốn đọc hoặc chỉnh sửa phần tử HTML.. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM. It is used almost every time you want to read or edit an HTML element.