Domanda

Sto cercando di usare jQuery per identificare < td > elementi che contengono testo di commento HTML specifico. Lo standard & Quot;: contiene & Quot; il selettore non sembra guardare nei commenti. Il mio contesto qui è che voglio aggiungere dinamicamente alcuni contenuti allo stesso elemento td. Ecco un esempio del target td:

<TD valign="top" class="ms-formbody" width="400px">
        <!-- FieldName="Title"
             FieldInternalName="Title"
             FieldType="SPFieldText"
          -->
            <span dir="none">
        <input name="ctl00$m$g_0b1e4ca3_9450_4f3e_b3af_8134fe014ea5$ctl00$ctl04$ctl00$ctl00$ctl00$ctl04$ctl00$ctl00$TextField" type="text" maxlength="255" id="ctl00_m_g_0b1e4ca3_9450_4f3e_b3af_8134fe014ea5_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField" title="Title" class="ms-long" /><br>
    </span>


        </TD>

Questo html è generato da SharePoint come parte di un nuovo modulo di elenco e voglio davvero trovare il td nel mio documento il cui " FieldInternalName " nel commento corrisponde una stringa di testo. Come posso usare jQuery (o qualsiasi javascript sarebbe davvero a posto) per trovare al meglio questi elementi td?

È stato utile?

Soluzione

Potresti afferrare il innerHTML dell'elemento ed eseguire un'espressione regolare su di esso.

Esempio:

// HTML: <div id="myElement">Lorem <!-- a comment --> ipsum.</div>

var element = document.getElementById('myElement');
alert(element.innerHTML); // alerts "Lorem <!-- a comment --> ipsum."
// here you could run a regular expression

Aggiornamento: un esempio più completo (con jQuery):

var tds = $('td'), match;
tds.each(function(index, element) {
    match = $(element).innerHTML.match(/FieldInternalName="(^["])"/);
    if (match) alert(match);
});

Altri suggerimenti

if(document.getElementById('myElement').innerHTML.indexOf(text_string) != -1) { //do something }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top