5種取值方式:1、用「元素物件.text()」取得指定元素的文字內容;2、用「元素物件.html()」取得元素中包含標籤的內容;3 、用「輸入框元素.val()」取得使用者輸入內容;4、用「元素物件.attr("屬性名稱")」取得指定屬性的值等。
本教學操作環境:windows7系統、jquery3.2.1版本、Dell G3電腦。
jquery中取值方式有多種方式,以下跟大家介紹一下。
1、取得元素的內容值的兩種方法:text()或html()
text() 方法可以回傳被選元素的文字內容。
html() 方法可以傳回被選取元素的內容(innerHTML)。
範例:使用text()取得文字內容,使用html()取得包含標籤的內容
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-3.2.1.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { console.log($("p").text()); console.log($("p").html()); }); }); </script> </head> <body> <button>获取p元素的内容</button> <p>这是一个<b>段落</b>。</p> </body> </html>
2.取得input輸入框的輸入值
input輸入框的輸入值由value 屬性控制。可以直接利用val() 方法來獲取,也可以attr("value") 方法來取得。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-3.2.1.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { console.log($("input[type=text]").val()); console.log($("input[type=password]").attr("value")); }); }); </script> </head> <body> <button>获取input元素的内容</button> <p>用户名: <input type="text" name="user" value="李华" /></p> <p>密 码: <input type="password" name="password" value="123456" /></p> </body> </html>
3、取得元素屬性值
#元素屬性值可以透過兩種方法取得:
attr() 方法
prop() 方法
區別:具有true 和false 這兩種取值的布林屬性,如checked、selected 和disabled 等,建議使用prop() 方法來操作,而其他的屬性都建議使用attr() 方法來操作。
範例1:使用attr()取得普通屬性的值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-3.2.1.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { console.log($("input").attr("name")); }); }); </script> </head> <body> <button>获取name属性值</button> <p>用户名: <input type="text" name="user" value="李华" /></p> </body> </html>
範例2:使用prop()取得布林屬性的值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-3.2.1.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { console.log($("input").prop("disabled")); }); }); </script> </head> <body> <button>获取disabled属性值</button> <p>密 码: <input type="password" name="password" value="123456" disabled /></p> </body> </html>
【推薦學習:jQuery影片教學、web前端影片】
以上是jquery有哪些取值方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Include:1)AsteeplearningCurvedUetoItsVasteCosystem,2)SeochallengesWithClient-SiderEndering,3)潛在的PersperformanceissuesInsuesInlArgeApplications,4)ComplexStateStateManagementAsappsgrow和5)TheneedtokeEedtokeEedtokeEppwithitsrapideDrapidevoltolution.thereedtokeEppectortorservolution.thereedthersrapidevolution.ththesefactorsshesssheou

reactischallengingforbeginnersduetoitssteplearningcurveandparadigmshifttocoment oparchitecent.1)startwithofficialdocumentationforasolidFoundation.2)了解jsxandhowtoembedjavascriptwithinit.3)

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

javascriptfatigueinrectismanagbaiblewithstrategiesLike just just in-timelearninganning and CuratedInformationsources.1)學習whatyouneedwhenyouneedit

tateractComponents通過theusestatehook,使用jestandReaCtTestingLibraryToSigulationsimintionsandIntractions and verifyStateChangesInTheUI.1)underthecomponentAndComponentAndComponentAndConconentAndCheckInitialState.2)模擬useruseruserusertactionslikeclicksorformsorformsormissions.3)

KeysinreactarecrucialforopTimizingPerformanceByingIneFefitedListupDates.1)useKeyStoIndentifyAndTrackListelements.2)避免使用ArrayIndicesasKeystopreventperformansissues.3)ChooSestableIdentifierslikeIdentifierSlikeItem.idtomaintainAinainCommaintOnconMaintOmentStateAteanDimpperperFermerfermperfermerformperfermerformfermerformfermerformfermerment.ChosestopReventPerformissues.3)

ReactKeySareUniqueIdentifiers usedwhenrenderingListstoimprovereConciliation效率。 1)heelPreactrackChangesInListItems,2)使用StableanDuniqueIdentifiersLikeItifiersLikeItemidSisRecumended,3)避免使用ArrayIndicesaskeyindicesaskeystopreventopReventOpReventSissUseSuseSuseWithReRefers和4)

獨特的keysarecrucialinreactforoptimizingRendering和MaintainingComponentStateTegrity.1)useanaturalAlaluniqueIdentifierFromyourDataiFabable.2)ifnonaturalalientedifierexistsistsists,generateauniqueKeyniqueKeyKeyLiquekeyperaliqeyAliqueLiqueAlighatiSaliqueLiberaryLlikikeuuId.3)deversearrayIndiceSaskeyseSecialIndiceSeasseAsialIndiceAseAsialIndiceAsiall


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能