第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

無(wú)法從模板操作 DOM 以添加/刪除屬性

無(wú)法從模板操作 DOM 以添加/刪除屬性

Go
慕姐4208626 2022-10-10 19:36:09
在我的代碼中,我想根據(jù)用戶(hù)身份驗(yàn)證級(jí)別禁用一些輸入字段,我可以在 JavaScript 中做到這一點(diǎn),但這是不推薦的解決方案,我想從服務(wù)器端做到這一點(diǎn)。以下是我的go代碼;package mainimport (    "context"    "html/template"    "net/http"    "strings"    "time")type User struct {    Flags string    Title string}type UsersPageData struct {    PageTitle string    Users     []User}func requestTime(next http.Handler) http.Handler {    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {        ctx := r.Context()        ctx = context.WithValue(ctx, "requestTime", time.Now().Format(time.RFC3339))        r = r.WithContext(ctx)        next.ServeHTTP(w, r)    })}func helloHandler(name string) http.Handler {    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {        var title string        if requestTime := r.Context().Value("requestTime"); requestTime != nil {            if str, ok := requestTime.(string); ok {                title = "\ngenerated at: " + str            }        }        master := strings.Join([]string{"admin", "user", "superuser", "read"}, " ")        admin := strings.Join([]string{"admin"}, " ")        user := strings.Join([]string{"user"}, " ")        tmpl := template.Must(template.ParseFiles("index.html"))        data := UsersPageData{            PageTitle: "Users list: " + title,            Users: []User{                {Flags: master, Title: "Everything"},                {Flags: admin, Title: "Administrator"},                {Flags: user, Title: "Normal user"},            },        }        tmpl.Execute(w, data)    })}func main() {    http.Handle("/john", requestTime(helloHandler("John")))    http.ListenAndServe(":8080", nil)}這是我的模板,包括 JS 代碼;<style>.admin {    color: green;}.user {    color: red;    --btn-disable: 0;}[data-authorized="no"] {  /* Attribute has this exact value */        cursor: not-allowed;        pointer-events: none;        /*Button disabled - CSS color class*/        color: #c0c0c0;        background-color: rgb(229, 229, 229) !important;}
查看完整描述

1 回答

?
ibeautiful

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊

在完整代碼下方,借助模板自定義函數(shù),我能夠做到這一點(diǎn):


package main


import (

    "html/template"

    "net/http"

)


type User struct {

    Flags []flag //string

    Title string

}


type UsersPageData struct {

    PageTitle string

    Users     []User

}


type flag int


const (

    Admin flag = iota + 1 // iota = 0

    Editer

    Superuser

    Viewer

    Dummy

)


func subtract(arg1, arg2 int) int {

    return arg1 - arg2

}


func helloHandler(name string) http.Handler {

    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

        // Map the `subtract` custom function in the template

        funcMap := map[string]interface{}{"subtract": subtract}


        master := []flag{Admin, Editer, Superuser, Viewer}

        admin := []flag{Admin, Superuser, Viewer}

        user := []flag{Viewer, Dummy}


        tmpl := template.New("").Funcs(template.FuncMap(funcMap))

        template.Must(tmpl.ParseFiles("index.html"))

        data := UsersPageData{

            PageTitle: "Users list: ",

            Users: []User{

                {Flags: master, Title: "Everything"},

                {Flags: admin, Title: "Administrator"},

                {Flags: user, Title: "Normal user"},

            },

        }

        tmpl.ExecuteTemplate(w, "index.html", data)

    })

}


func main() {

    fs := http.StripPrefix("/www/", http.FileServer(http.Dir("./www")))

    http.Handle("/www/", fs)


    http.Handle("/", helloHandler("John"))

    http.ListenAndServe(":8080", nil)

}

是index.html:


<html>

{{/* This is a comment 

{{$flags := []flag{Admin, Editer, Superuser, Viewer};}} 

    Admin Flag = iota + 1 // iota = 0

    Editer

    Superuser

    Viewer

    }}

*/}}


<ul>

    {{range .Users}}

        <span>{{.Title}}</span>

        {{ $done := false}} {{$length := len .Flags}}

        {{range $i, $v := .Flags}}

            {{ if $done }}

            {{ else }}

                {{if or (eq $v 1) (eq $v 3)}} 

                    <input type="text" name="subject" placeholder= {{$v}} required>

                    {{ $done = true }}

                {{else}}

                    {{ if eq $i (subtract $length 1)}}

                        <input type="text" name="subject" placeholder= {{$v}} disabled>

                    {{ end }}

                {{end}}

            {{end}}

        {{end}}

    {{end}}

</ul>

</html>

http://img1.sycdn.imooc.com//634403d20001f7f114970378.jpg

查看完整回答
反對(duì) 回復(fù) 2022-10-10
  • 1 回答
  • 0 關(guān)注
  • 123 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)