簡體   English   中英

獲取以前的 Html 跨度

[英]Get previous Span in Html

嗨,我正在嘗試獲取用作標簽的 html 中的前一個跨度,我嘗試過 jquery .prev([selector]),但選擇器不起作用,它返回輸入字段的父跨度我得到。

這是我的 HTML 和腳本:

<form>
<span class="a-text-bold">
    Add product detail
</span>

<div class="a-section a-spacing-base">
    <div class="a-section a-spacing-mini">
        <span class="a-text-bold">
        Brand name
        </span>
    </div>
    <span class="a-declarative" data-action="form-item-action">
        <span class="a-declarative">
        <input type="text" maxlength="50" value="Bubble Goth Babes" id="data-draft-brand-name" autocomplete="off" name="data[draft][brand_name]" class="a-input-text a-form-normal  gear-filter-control-characters-textbox  lock-needs-hidden">
        </span>
    </span>
</div>
<div class="a-section a-spacing-base">
    <div class="a-section a-spacing-mini">
        <span class="a-text-bold">
        Title of product
        </span>
    </div>
    <span class="a-declarative" data-action="form-item-action">
        <span class="a-declarative">
        <input type="text" maxlength="50" value="Cute Zombkitty Zombie Kitten Neon Funny Brain Cat" id="data-draft-name-en-us" autocomplete="off" name="data[draft][brand_name]" class="a-input-text a-form-normal  gear-filter-control-characters-textbox  lock-needs-hidden">
        </span>
    </span>
</div>
    <button onclick="getLabelAndValue()">Get</button>
</form>



<script type="text/javascript">

    function getLabelAndValue(){

        var focused = document.activeElement;

        var form = document.getElementsByName(focused.form.name);

        for (var i = 0; i < form.length; i++) {
            if(form[i].tagName.toLowerCase() == "input" && form[i].getAttribute('type').toLowerCase() == "text"){
                console.log($("#" + form[i].id).prev("span .a-text-bold").text()); //Doesn't work
                console.log(form[i].value); //Already working, means I'm pointing to right element
            }
        }

    }

</script> 

我想在 console.log 上看到的是:

//Brand Name
//Bubble Goth Babes

//Title of Product
//Cute Zombkitty Zombie Kitten Neon Funny Brain Cat

注意:我已經能夠獲得表單元素值( form[i].value ),只是它之前的跨度與 className a-text-bold是我需要獲得的。

我會堅持使用 jQuery,因為它非常易於使用。 這基本上是相同的代碼,只是以不同的方式獲取標簽。 請注意,您不能使用prev() ,因為span不是input的同級。 您必須將 DOM 向上移動到一個共同的祖先,然后向下返回span

 function getLabelAndValue(e) { e.preventDefault(); var form = e.target.form; var inputs = $('input[type="text"]', form); inputs.each(function (input) { // get the closest common ancestor, then find the "a-text-bold" span. var label = $(this).closest(".a-section").find(".a-text-bold").text().trim(); console.log(label); console.log(this.value); //Already working, means I'm pointing to right element }); } window.onload = function() { document.getElementById("get").addEventListener("click", getLabelAndValue); };
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <span class="a-text-bold"> Add product detail </span> <div class="a-section a-spacing-base"> <div class="a-section a-spacing-mini"> <span class="a-text-bold"> Brand name </span> </div> <span class="a-declarative" data-action="form-item-action"> <span class="a-declarative"> <input type="text" maxlength="50" value="Bubble Goth Babes" id="data-draft-brand-name" autocomplete="off" name="data[draft][brand_name]" class="a-input-text a-form-normal gear-filter-control-characters-textbox lock-needs-hidden"> </span> </span> </div> <div class="a-section a-spacing-base"> <div class="a-section a-spacing-mini"> <span class="a-text-bold"> Title of product </span> </div> <span class="a-declarative" data-action="form-item-action"> <span class="a-declarative"> <input type="text" maxlength="50" value="Cute Zombkitty Zombie Kitten Neon Funny Brain Cat" id="data-draft-name-en-us" autocomplete="off" name="data[draft][brand_name]" class="a-input-text a-form-normal gear-filter-control-characters-textbox lock-needs-hidden"> </span> </span> </div> <button id="get">Get</button> </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM